With Microsoft.NET Framework 2.0 everything is asynchronous and we can send mail also asynchronously. This features is very useful when you send lots of bulk mails like news letter. You don’t have to wait for response from mail server and you can do other task .
Let's create a simple example to send mail. For sending mail asynchronously you need to create a event handler that will notify that mail successfully sent or some errors occurred during sending mail. Let’s create a mail object and then we will send it asynchronously. You will require System.Net.Mail space to import in your page to send mail.
//you require following name space
using System.Net.Mail;
//creating mail message object
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("Put From mail address here");
mailMessage.To.Add(new MailAddress("Put To Email Address here"));
mailMessage.CC.Add(new MailAddress("Put CC Email Address here"));
mailMessage.Bcc.Add(new MailAddress("Put BCC Email Address here"));
mailMessage.Subject = "Put Email Subject here";
mailMessage.Body = "Put Email Body here ";
mailMessage.IsBodyHtml = true;//to send mail in html or not
SmtpClient smtpClient = new SmtpClient("Put smtp server host here", 25);//portno here
smtpClient.EnableSsl = false; //True or False depends on SSL Require or not
smtpClient.UseDefaultCredentials = true ; //true or false depends on you want to default credentials or not
Object mailState = mailMessage;
//this code adds event handler to notify that mail is sent or not
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
try
{
smtpClient.SendAsync(mailMessage, mailState);
}
catch (Exception ex)
{
Response.Write(ex.Message);
Response.Write(ex.StackTrace);
}
void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
MailMessage mailMessage = e.UserState as MailMessage;
if (!e.Cancelled && e.Error != null)
{
Response.Write("Email sent successfully");
}
else
{
Response.Write(e.Error.Message);
Response.Write(e.Error.StackTrace);
}
}
Technorati Tags: System.Net.Mail,ASP.NET Mail
if I want to send a thousand of email from web page then it takes so much time to postback the page, do you have any other solution for this?
ReplyDeleteWhy just not use ThreadPool.QueueUserWorkItem() method?
ReplyDeletehi
ReplyDeletei have tried your code it give this error
any idea how to resolve this
Failure sending mail. at System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken)
Hi Can you send me your code please. Because its working fine. Did you configured your smtp settings properly?
ReplyDeleteyes , here its
ReplyDelete//creating mail message object
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("mymail@gmail.com");
mailMessage.To.Add(new MailAddress("yyyyyy@gmail.com"));
mailMessage.CC.Add(new MailAddress("xxxxxxxx@gmail.com"));
mailMessage.Bcc.Add(new MailAddress("zzzzzzzz@gmail.com"));
mailMessage.Subject = "Email Checking Asynchronously";
mailMessage.Body = "Email test asynchronous";
mailMessage.IsBodyHtml = true;//to send mail in html or not
SmtpClient smtpClient = new SmtpClient();//portno here
smtpClient.Host = "smtp.gmail.com";
smtpClient.EnableSsl = true ; //True or False depends on SSL Require or not
smtpClient.Credentials = new NetworkCredential("mymail@gmail.com", "mypassword");
//smtpClient.UseDefaultCredentials = true; //true or false depends on you want to default credentials or not
Object mailState = mailMessage;
//this code adds event handler to notify that mail is sent or not
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
try
{
smtpClient.SendAsync(mailMessage, mailState);
}
catch (Exception ex)
{
Response.Write(ex.Message);
Response.Write(ex.StackTrace);
}
}
void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
MailMessage mailMessage = e.UserState as MailMessage;
if (!e.Cancelled && e.Error != null)
{
Response.Write("Email sent successfully");
}
else
{
Response.Write(e.Error.Message);
Response.Write(e.Error.StackTrace);
}
}
Hi Vekant,
ReplyDeleteWhen you write
smtpClient.SendAsync(mailMessage, mailState);
After this code will not wait because it async so whenever the operation is completed it will fire that event and you can continue work on other task after this line.
Also please tell me what is the error are you giving right credentials.
ReplyDeletehi i got the answer ie: i posted the issue on forums i got it
ReplyDeleteso you have to change the code
on page_Directive Async="true"
then change this code its working. so do the same on your post too.
void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
MailMessage mailMessage = e.UserState as MailMessage;
if (e.Cancelled || e.Error != null)
{
Response.Write(e.Error.Message);
Response.Write(e.Error.StackTrace);
}
else
{
Response.Write("Email sent successfully");
}
}
any sample is I use it in asp.net web ?? How can I show message to user in page.aspx when it execute smtpClient_SendCompleted event ?? thanks.
ReplyDelete