Created
November 17, 2014 12:24
-
-
Save graphnode/53f8c58e520d5de59a9e to your computer and use it in GitHub Desktop.
Parallel Email sending with throttling.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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