How to send mail asynchronously in ASP.NET

No.of Views903
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  14 May 2010 09:05:27
Tag : ASP.NET , Email
With Microsoft.NET Framework 2.0 everything is asynchronous and we can send mail also asynchronously
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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 .

Implementation

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.

C# Code

//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);
}

Following is a code for event handler

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);
 }
}

So that’s it you can send mail asynchronously this 30-40 lines of code.Hope helps to all you.

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Expert
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
    The Email is important in the web site. When you are create a new website you have to implement in the email sending features as well. So in this article I will explain how to send email using ASP.NET with SMTP.
    Published Date : 06/Dec/2010
    In this article, I going to explain that how to perform the validation for email address in HTML 5 using inbuilt features in it. In addition, the HTML 5 has many features those related with user input validations.
    Published Date : 27/Mar/2012
    In this article, we will focus how to use the Gmail Account details to send email in asp.net with System.NET.Mail. The Email is a compulsory part of the web applications to send information to users or other way direction.
    Published Date : 30/Dec/2010
    This lesson focuses on how to send mail messages in .NET Framework via a SMTP server. It firstly discusses the techniques which .NET Framework provides you to send mail messages. After that, it discusses types available for you when working with SMTP servers. Next, it discusses how to implement these techniques and to send mails from a .NET client. At the end of this lesson, there is a sample application, Geming Mail+, which is used to send mails from a various SMTP servers. This application
    Published Date : 16/Feb/2010
    Sending mails from gmail account using ASP.net
    Published Date : 15/Feb/2010
Comments
By:moditDate Of Posted:11/10/2011 8:55:30 PM
Error
Failure sending mail. at System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken) at SendMail.Button1_Click(Object sender, EventArgs e)
By:RRaveenDate Of Posted:11/20/2010 5:05:03 AM
Hi Ajay
Hi, Can you post your error detail in codegain message board, then we will try to solve asap.
By:ajayDate Of Posted:11/20/2010 3:16:43 AM
Regarding Send mail through asp.net
helo sir, i used your code to run email application.but it is giving excception while sending............ client.SendAsync(message, mailstate); in this line it is giving exception........... please help me out
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top