Skip to main content

Sending Email asynchronously in Asp.Net 2.0

Another of the new feature in asp.net 2.0 is the support to send emails asynchronously. This is a very important feature. With the help of this feature you don’t need to wait for the email to be sent before performing other tasks in the page. But instead these tasks can be performed while the mail is being sent asynchronously.

To send Emails asynchronously we need to wire up a sendComplete event, create a send complete event and then call the sendAsync event.

To do this first create an object and assign it the mail object. We can access this object in the call back.

object userState = mail;

Now we need to wire up the event when the async send is complete.

smtp.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted);

Now start the asynchronous call



smtp.SendAsync( mail, userState );

We have to write the wired-up method that will be invoked when the send is complete.

public static void SmtpClient_OnCompleted(object sender, AsyncCompletedEventArgs e)

{

MailMessage mail= (MailMessage)e.UserState;

string subject = mail.Subject;

if (e.Cancelled)

{ Console.WriteLine("Send canceled for mail with subject [{0}].", subject); }

if (e.Error != null)

{ Console.WriteLine("Error {1} occurred when sending mail [{0}] ", subject, e.Error.ToString()); }

else

{ Console.WriteLine("Message [{0}] sent.", subject ); }

That’s all you have to do to send the emails asynchronously

Comments

Popular posts from this blog

World Environment Day

This entry is in response to the Conserve, Recycle and Discover contest organised by blogadda.com in association with Pringoo ! World Environment Day, commemorated on 5 June since 1972, is one of the ways focuses world attention on the environment and encourages political action. People from countries all over the world have mobilized for individual and organized environmental action. Activities involve all sectors of society, governments, non- and inter-governmental organizations, businesses, industries, civil society, media and schools. Please take the following Pledge to save our environment. 1, I pledge to Recycle more, use less water and electricity. 2, I pledge to smoke less in 2010. 3, I pledge to send more electronic documents instead of using paper. 4, I pledge to continue using my bicycle to go to work (instead of my motorbike). 5, wash with full loads it is more energy efficient than doing several smaller ones. 6, Incite of dryer I will use hang dry for energy sav...

GridView Alphabet Paging

Introduction: GridView paging feature allow us to display fixed number of records on the page and browse to the next page of records. Although paging is a great feature but sometimes we need to view all the items alphabetically. The idea behind this article is to provide a user with a list of all the alphabets and when the user clicks on a certain alphabet then all the records starting with that alphabet will be populated in the GridView control. Populating the GridView Control: The first task is to populate the GridView control. I will be using the Northwind database in my article which, is installed by default for SQL SERVER 2000 and SQL SERVER 7 databases. The code below is used to populate the GridView control. private void BindData() { string connectionString = "Server=localhost;Database=Northwind;Trusted_Connection=true"; SqlConnection myConnection = new SqlConnection(connectionString); SqlDataAdapter ad = new SqlDataAdapter("SELECT P...

TED

TED is a nonprofit devoted to Ideas Worth Spreading. It began in 1984 as an annual conference devoted to Technology, Entertainment and Design -- hence TED -- but TED's reach, and its scope, have become ever broader since then. TEDTalks cover science, arts, politics, global issues, architecture, music and more. Speakers come from a wide variety of communities and disciplines -- people like Bill Clinton, Nobel laureate Murray Gell-Mann, Wikipedia co-founder Jimmy Wales, and Google co-founders Sergey Brin and Larry Page. The TED Conference itself takes place in Long Beach each spring, with a simulcast event in Palm Springs. In 2009, we'll also host a summer conference in Oxford, UK, called TEDGlobal, as well as a conference in fall 2009 called TEDIndia, in Mysore, India. Anyone can apply to attend a TED Conference! If you want to come, apply for an invitation. More info here: http://www.ted.com/conferences