Asynchronously sending a System.Net.Mail.MailMessage in C#

When sending an email in your ASP.NET application there are times when you do not want the user experience to slow just to wait for an email to be sent.  The code sample below is how to send a System.Net.Mail.MailMessage asynchronously so that the current thread can continue while a secondary thread sends the email. 

public static void SendEmail(System.Net.Mail.MailMessage m)
{
    SendEmail(m, true);
}

public static void SendEmail(System.Net.Mail.MailMessage m, Boolean Async)
{

    System.Net.Mail.SmtpClient smtpClient = null;

    smtpClient = new System.Net.Mail.SmtpClient("localhost");

        if (Async)
        {
            SendEmailDelegate sd = new SendEmailDelegate(smtpClient.Send);
            AsyncCallback cb = new AsyncCallback(SendEmailResponse);
            sd.BeginInvoke(m, cb, sd);
        }
        else
        {
            smtpClient.Send(m);
        }

}

private delegate void SendEmailDelegate(System.Net.Mail.MailMessage m);

private static void SendEmailResponse(IAsyncResult ar)
{
        SendEmailDelegate sd = (SendEmailDelegate)(ar.AsyncState);

        sd.EndInvoke(ar);
}

To use this just call the SendEmail() method with System.Net.Mail.MailMessage object.

 



Published 12 October 2009 09:53 PM by Jeff Widmer
Filed under: ,

Comments

# Padma said on 13 October, 2009 02:13 AM

Nice article. Can u please explain little bit about "Asynchronous" call

Many Thanks

# Sending Async Email in Asp.Net « Sandip’s Programming Zen said on 13 October, 2009 05:26 AM

Pingback from  Sending Async Email in Asp.Net « Sandip’s Programming Zen

# WebDevVote.com said on 13 October, 2009 10:29 PM

You are voted (great) - Trackback from WebDevVote.com

# RichardD said on 14 October, 2009 01:56 PM

What's wrong with:

smtpClient.SendAsync(m, null);

"Sends the specified e-mail message to an SMTP server for delivery. This method does not block the calling thread and allows the caller to pass an object to the method that is invoked when the operation completes."

msdn.microsoft.com/.../x5x13z6h.aspx

www.systemnetmail.com/.../4.6.aspx

# Jeff Widmer said on 14 October, 2009 02:59 PM

Hey RichardD,

I didn't know that SmtpClient.SendAsync existed.  I wrote the above code back in .NET 1.0 days and it looks like SendAsync was introduced in .NET 2.0.  Cool!  Thanks for info... I will make a note to possibly rewrite this sample with SendAsync instead.

Thanks!

-Jeff

# Jeff Widmer said on 14 October, 2009 03:03 PM

Hi Padma,

Asynchronous means that the current thread will not wait while the email is getting sent.  So if you send the email Synchronously, then the thread will wait for the email to send... which could be several seconds depending on your mail server.  Sending the email Asynchronously means the current thread will continue on with whatever it was doing and the email will send on another thread.  For web applications making the customer wait for several seconds can be a bit too long, and then taking into account that many customers could all be waiting for the same action, could mean a lot of waiting going on for something that is probably not a necessary action for the web application (such as sending a confirmation email).

Hope this helps,

-Jeff

# ContinousLearner: Links (10/15/2009) | Astha said on 15 October, 2009 08:13 PM

Pingback from  ContinousLearner: Links (10/15/2009) | Astha

# razvantim said on 16 October, 2009 03:45 PM

thank you for a great tip on sending email (or doing other heavy lifting on the back end of the website)

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Search

Go

This Blog

News

    Ads by Lake Quincy Media
    Ads by Lake Quincy Media

Recommended Blogs

Syndication