PowerTCP Mail for .NET
Send(MailMessage) Method
Example 




MailMessage representing an email message.
Send a MailMessage to a mail server.
Syntax
Public Overloads Function Send( _
   ByVal message As MailMessage _
) As SmtpResult
Dim instance As Smtp
Dim message As MailMessage
Dim value As SmtpResult
 
value = instance.Send(message)
public SmtpResult Send( 
   MailMessage message
)
public: SmtpResult* Send( 
   MailMessage* message
) 
public:
SmtpResult^ Send( 
   MailMessage^ message
) 

Parameters

message
MailMessage representing an email message.

Return Value

SmtpResult that describes the result.
Exceptions
ExceptionDescription
ProtocolExceptionBad SMTP protocol response received from server.
System.Net.Sockets.SocketExceptionA socket failure.
System.InvalidOperationExceptionMailMessage.BitEncoding is TransferEncoding.EightBit or TransferEncoding.Binary and the server does not advertise 8bit or binary transport.
System.FormatExceptionBad address format.
Remarks

This method is used to send most email messages. Initialize a MailMessage as follows:

Alternatively, use Send(String,String,String,String) to send a basic text message, Send(MailMessage,String,String) to specify the mail addresses for the message envelope, or Send(Stream,String,String) to send a previously encoded message (stream).

This method automatically calls Connect and Authenticate if not connected to the server and SendDirectToMx is false.

Example
Uses the Smtp component to send a text message with an attachment.
private void sendMail(object sender)
{
    //Create message to send
    MailMessage message = new MailMessage();
    message.To = toAddress;
    message.From = fromAddress;
    message.Subject = "File Attached";
    message.Text = "Please see the attached file.";

    //Add the attachment
    message.Attachments.Add(new Attachment(Application.StartupPath + "\\myImage.jpg"));

    //Set session parameters
    smtp1.Session.RemoteEndPoint = 
        new Dart.Mail.IPEndPoint(myMailServer, Smtp.GetDefaultPort(smtp1.Session));
    smtp1.Session.Username = myUsername;
    smtp1.Session.Password = myPassword;

    //Send message
    smtp1.Send(message);

    //Logout gracefully
    smtp1.Close();
}

private void smtp1_Progress(object sender, SmtpProgressEventArgs e)
{
    //Update progress bar as message is sent
    progressBar1.Value = (e.Final) ? 0 : (int)((e.Position * 100) / e.Length);
}
Private Sub sendMail(ByVal sender As Object)
    'Create message to send
    Dim message As New MailMessage()
    message.To = toAddress
    message.From = fromAddress
    message.Subject = "File Attached"
    message.Text = "Please see the attached file."

    'Add the attachment
    message.Attachments.Add(New Attachment(Application.StartupPath & "\myImage.jpg"))

    'Set session parameters
    smtp1.Session.RemoteEndPoint = New Dart.Mail.IPEndPoint(myMailServer, Smtp.GetDefaultPort(smtp1.Session))
    smtp1.Session.Username = myUsername
    smtp1.Session.Password = myPassword

    'Send message
    smtp1.Send(message)

    'Logout gracefully
    smtp1.Close()
End Sub

Private Sub smtp1_Progress(ByVal sender As Object, ByVal e As SmtpProgressEventArgs) Handles smtp1.Progress
    'Update progress bar as message is sent
    progressBar1.Value = If(e.Final, 0, CInt((e.Position * 100) \ e.Length))
End Sub
See Also

Reference

Smtp Class
Smtp Members
Overload List


PowerTCP Mail for .NET Documentation Version 4.3
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic