Send mail async in Asp.net 2.0
Now you can send email asynchronously with ASP.NET 2.0, here i have given step
by step implementation
In page directive set the
async=true.
<%@
Page Language="C#" MasterPageFile="~/MasterPages/CaseBasics.Master"
AutoEventWireup="true"
Codebehind="Pagename.aspx.cs"
Inherits="Core.folder1.Pagename"
Async="true"
%>
Do the following in mail sending class.
using System.Net.Mail;
using System.ComponentModel;
public bool
SendEmail()
{
MailMessage mailMessage =
new MailMessage();
this.mailMessage.From =
new MailAddress(this.senderEmailAddress,
this.senderName);
this.mailMessage.IsBodyHtml =
true;
this.smtpClient.EnableSsl =
false;
this.smtpClient.Host = server.SMTP.Server
;
object userState =
this.mailMessage;
this.smtpClient.SendCompleted +=new
SendCompletedEventHandler(smtpClient_SendCompleted);
this.smtpClient.SendAsync(this.mailMessage, userState);
}
public
static void smtpClient_SendCompleted(object sender,
AsyncCompletedEventArgs e)
{
MailMessage mail = (MailMessage)e.UserState;
string subject = mail.Subject;
if (e.Cancelled)
{
//send cancelled
}
if (e.Error !=
null)
{
//errro occured
}
else
{
// mail sent successfully
}
}