Skip to content

Instantly share code, notes, and snippets.

@njmube
Created December 5, 2015 04:51
Show Gist options
  • Save njmube/8de81efacabf2bd59713 to your computer and use it in GitHub Desktop.
Save njmube/8de81efacabf2bd59713 to your computer and use it in GitHub Desktop.
Sending multiple emails in parallel using SmtpClient.SendMailAsync with async and Task.WhenAll
var sendEmailTasks = recipientList.Select(async recipient =>
{
var message = new MailMessage { To = { recipient }, ... };
// instantiate a new client for each mail because of this:
// http://www.codefrenzy.net/2012/01/30/how-asynchronous-is-smtpclient-sendasync/
using (var smtpClient = new SmtpClient())
{
await smtpClient.SendMailAsync(message);
}
});
await Task.WhenAll(sendEmailTasks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment