Sending Email Asynchronously
Good Morning to Everybody this is my First article.
Now i am going to see how to send email Asynchronously, actually we send email normally using asp.net Example check here..
Send Email Using ASP.NET
By default smtpClient has one method called SendAsync to send an email ansynchronously. so if you sending an email normally it will take some time for each mail sent. But if you send an email Asynchronously it will not wait for each mail sent it will do the other process , while mail sending is done at background process.
So Here is the code.
//creating mail message object MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress("venkat@gmail.com"); mailMessage.To.Add(new MailAddress("msdotnettechies@gmail.com")); mailMessage.CC.Add(new MailAddress("user1@gmail.com")); mailMessage.Bcc.Add(new MailAddress("user2@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("yourGmail@gmail.com", "yourpassword"); //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); } // this is the event called on main method 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"); } }
Ref : http://jalpesh.blogspot.com/2010/02/how-to-send-mail-asynchronously-in.html
Read more »