Skip to content

Instantly share code, notes, and snippets.

@graphnode
Created November 17, 2014 12:24
Show Gist options
  • Save graphnode/53f8c58e520d5de59a9e to your computer and use it in GitHub Desktop.
Save graphnode/53f8c58e520d5de59a9e to your computer and use it in GitHub Desktop.
Parallel Email sending with throttling.
pyblic async Task SendEmailsWithThrottling(List<string> totalRecipientList, int throttle)
{
for(var i = 0; i < Math.Ceil(totalRecipientList.Count / throttle); i++)
{
var recipientList = totalRecipientList.Skip(i * throttle).Take(throttle);
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