System.Net.Mail FAQ

Complete FAQ Listing


4.6 How do I send email asynchronously? Printer Friendly

System.Net.Mail has also added asynchronous support for sending email. To send asynchronously, you need need to
  1. Wire up a SendCompleted event
  2. Create the SendCompleted event
  3. Call SmtpClient.SendAsync

These steps are demonstrated in the code below.

[ C# ]

        static void SendAsync()
        {
            //create the mail message
            MailMessage mail = new MailMessage();

            //set the addresses
            mail.From = new MailAddress("me@mycompany.com");
            mail.To.Add("you@yourcompany.com");

            //set the content
            mail.Subject = "This is an email";
            mail.Body = "this is the body content of the email.";

            //send the message
            SmtpClient smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
            //the userstate can be any object. The object can be accessed in the callback method
            //in this example, we will just use the MailMessage object.
            object userState = mail; 

            //wire up the event for when the Async send is completed
            smtp.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted);

            smtp.SendAsync( mail, userState );
        }
        public static void SmtpClient_OnCompleted(object sender, AsyncCompletedEventArgs e)
        {
            //Get the Original MailMessage object
            MailMessage mail= (MailMessage)e.UserState;

           //write out the subject
            string subject = mail.Subject;

            if (e.Cancelled)
            {
                Console.WriteLine("Send canceled for mail with subject [{0}].", subject);
            }
            if (e.Error != null)
            {
                Console.WriteLine("Error {1} occurred when sending mail [{0}] ", subject, e.Error.ToString());
            }
            else
            {
                Console.WriteLine("Message [{0}] sent.", subject );
            }
        }


[ VB.NET ]
    Sub SendAsync()
        'create the mail message
        Dim mail As New MailMessage()

        'set the addresses
        mail.From = New MailAddress("me@mycompany.com")
        mail.To.Add("you@yourcompany.com")

        'set the content
        mail.Subject = "This is an email"
        mail.Body = "this is the body content of the email."

        'send the message
        Dim smtp As New SmtpClient("127.0.0.1") 'specify the mail server address
        'the userstate can be any object. The object can be accessed in the callback method
        'in this example, we will just use the MailMessage object.
        Dim userState As Object = mail

        'wire up the event for when the Async send is completed
        AddHandler smtp.SendCompleted, AddressOf SmtpClient_OnCompleted

        smtp.SendAsync(mail, userState)
    End Sub 'SendAsync

    Public Sub SmtpClient_OnCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
        'Get the Original MailMessage object
        Dim mail As MailMessage = CType(e.UserState, MailMessage)

        'write out the subject
        Dim subject As String = mail.Subject

        If e.Cancelled Then
            Console.WriteLine("Send canceled for mail with subject [{0}].", subject)
        End If
        If Not (e.Error Is Nothing) Then
            Console.WriteLine("Error {1} occurred when sending mail [{0}] ", subject, e.Error.ToString())
        Else
            Console.WriteLine("Message [{0}] sent.", subject)
        End If
    End Sub 'SmtpClient_OnCompleted