Sandip's Programming Zen

An attempt to share tech/coding experiences

Sending Async Email in Asp.Net

leave a comment »

Found an interesting piece of code here.

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);

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

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


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


Written by Sandip

October 13, 2009 at 9:26 am

Posted in .Net, Asp.Net, Programming

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: