this blog contains information for .net and sql stuffs. You can find various tips and tricks to overcome problem you may be facing in ...

Monday, December 7, 2009

How to send email Asynchronously

When Our smtp Serve does not respond to immediately at that time it takes time to send email. So it happens that our application does not respond to user and user shut down our application. In this case its wise that we should send email Asynchronously and when email send successfully we just give notification it send successfully or not sent what so ever. This all things are available in the .net framework 2.0.

I am assuming that we know how to send email like to create MailMessage object then add subject,From and To address, body ,attachment etc..

Then create object of SmtpClient to send email.

After the send method call SendAsync method and pass the MailMessage Object also wire up method to SendComplete event.

//Send email

SmtpClient sc = new SmtpClient(“”);

//we have MailMessage Object mm.


//Wire up event of SendCompleted

sc.SendCompleted += new SendCompletedEventHandler(sc_SendCompleted);


In the method of sc_SendCompleted we can cancel to sending of email, whether email is sent successfully or email encountered any error to deliver etc.

//Write method for sc_SendCompleted

void sc_SendCompleted(object sender, AsyncCompletedEventArgs e)

{ if(e.Cancelled)


MessageBox.Show(“Message sending is Cancelled”);


else if(e.Error != null)


MessageBox.Show(“Error to Send Email Error is:” + e.Error.ToString() );




MessageBox.Show(“Message sent Successfully”);



No comments: