Click here to Skip to main content
15,881,651 members
Articles / Web Development / ASP.NET

Send asynchronous mail using asp.net

Rate me:
Please Sign up or sign in to vote.
4.83/5 (4 votes)
27 Mar 2015CPOL 46.2K   28   2
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.

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) { /* exception handling code here */ }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
QuestionNot working. Pin
Slow Eddie13-May-21 11:05
professionalSlow Eddie13-May-21 11:05 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun30-Mar-15 2:39
Humayun Kabir Mamun30-Mar-15 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.