Home / ASP.NET Wiki / .NET Framework Essentials / Send email with smtp authentication using ASP.NET 3.5 / Send asynchronous mail using asp.net

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.

Is that code working with error free..

http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx is not working

Almost... Read this: http://blog.jdconley.com/2009/01/fire-and-forget-email-webservices-and.html

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. abiruban (1)