How to send email in the Background in AsP.NET?
asp.net, email, sendmail
Solution
You can use the System.Net.Mail.SmtpClient class to send the email using the SendAsync() method.
var smtpClient = new SmtpClient();
var message = new MailMessage(fromAddress, toAddress, subject, body);
smtpClient.SendCompleted += new SendCompletedEventHandler(OnSendCompletedCallback);
smtpClient.SendAsync(message, null); // Null Or pass a user token to be send when the send is complete
If you need to handle perform some additional stuff after the async send is complete you can subscribe to the SendCompleted event of the SmtpClient as well.
private void OnSendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Handle the callback if you need to do anything after the email is sent.
}
Here is a link to the documentation on MSDN.
Problem
I am using ASP.NET Web forms, When a user submit a page, an email will be sent to many people which is slowing the post-back, what is the best way to send the emails without slowing the reloading of the page? thanks