This article describe how we can send async mail using ASP.Net
First Method takes "to","attachements","subject","body" as parameters and set within the logic itself.
You have to set all the credentials i.e "Username","password".
client.SendAsync(mailmessage, userState); => As this line gets execute running thread doesnt wait for the next instruction and it executes the next statement and so on.
And the following code make sense to callback the rest of logic:
- client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
- public string SendMail(string to, string[] attachments, string subject, string Body = "")
- {
- string userState = "Mail State";
- MailMessage mailmessage = new MailMessage("[email protected]", to, subject, Body);
- SmtpClient client = new SmtpClient();
- client.Port = 587;
- client.Host = "smtp.gmail.com";
- client.EnableSsl = true;
- client.UseDefaultCredentials = false;
- client.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
- client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
- NetworkCredential _NetworkCredentials = new NetworkCredential("[email protected]", "password");
- if (attachments != null)
- {
- // Add the attachments
- foreach (string attachment
- in attachments)
- mailmessage.Attachments.Add(new System.Net.Mail.Attachment(attachment));
- }
- client.SendAsync(mailmessage, userState);
- return "0";
- }
- private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
- {
- // Get the unique identifier for this asynchronous operation.
- String token = (string)e.UserState;
- if (e.Cancelled)
- {
- //MessageBox.Show("Mail Cancelled");
- }
- if (e.Error != null)
- {
- //Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
- }
- else
- {
- //MessageBox.Show("Mail Sent Successfully");
- }
- }

Rachit PatelPosted Mar 13, 2015, 5:55 AM
Your code working fine in web page when I set @page directory asyc="True" but it's not working in my console application. Can you please help me out.
Ravi KumarPosted Sep 26, 2012, 5:42 AM
Check out the following link for the same: http://aspnetcsharpcodes.blogspot.in/2012/09/how-to-send-email-in-aspnet-using-c.html
Kamal RawatPosted Sep 20, 2012, 9:44 AM
It was in one of my projects..u can use it without any difficulty.
Heiko ScholzePosted Sep 17, 2012, 11:28 AM
Whats the meaning of "_NetworkCredentials"? I don't see any using.