Send asynchronous mail using asp.net

 Rate It (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 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

Revision number 6, Tuesday, November 03, 2009 7:17:17 PM by mbanavige

Comments

Could you tell me why I need to use something or when I need to use this technic. thanks

http://msdn.microsoft.com/en-us/magazine/cc163463.aspx - Read that article to know benefits of asynchronous programming and then you will realize how much important sending asynchronous mail specially at the time of sending a bunch of mail at once.

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (7)
  2. yrb.yogi (5)
  3. srinivaskotra (3)
  4. XIII (2)
  5. rdmartin33 (2)
  6. Babunareshnarra (2)
  7. Dungimon (1)
  8. ali62b (1)
Microsoft Communities