Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Send asynchronous mail using asp.net

0.00/5 (No votes)
3 Nov 2009 1  
Sending e-mail is an important and common feature in ASP.Net (through the system.net.mail namespace). In this article, I will show how to send

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Sending e-mail is an important and common feature in ASP.Net (through the system.net.mail namespace). In this article, I will show how to send asynchronous mail, which is used, for example, to send bulk e-mail. ASP.Net includes the feature of asynchronous mail.

Following is an example of sending asynchronous mail:

    public void SendAsyncMail()
    {
        MailMessage mail = new MailMessage();

        mail.From = new MailAddress("Enter from mail address");
        mail.To.Add(new MailAddress("Enter to address #1"));
        mail.To.Add(new MailAddress("Enter to address #2"));
        mail.Subject = "Enter mail subject";
        mail.Body = "Enter mail body";

        SmtpClient smtpClient = new SmtpClient();
        Object state = mail;

        //event handler for asynchronous call
        smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
        try
        {
            smtpClient.SendAsync(mail, state);
        }
        catch (Exception ex)
        {
            
        }
    }
    void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {

        MailMessage mail = e.UserState as MailMessage;

        if (!e.Cancelled && e.Error!=null)
        {
            message.Text = "Mail sent successfully";
        }
    }

MSDN link : http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

Video Tutorial : http://www.asp.net/learn/videos/video-420.aspx

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here