Reputation: 45
Question : I have a list of 50~100 person i need send email to, MailMessage allows me to add a list of To(MailAddressCollection) and just smtpClient.Send(mail) or the Async method ? What is the best practice for sending email to multiple recipients ?
Upvotes: 1
Views: 1890
Reputation: 13112
From what I understand from your question, you're currently trying to decide which of these is better?
Adding all emails to a single MailMessage and send syncronously
var mailAddressCollection = new MailAddressCollection();
mailAddressCollection.Add("[email protected],[email protected]...");
// initialize the message and smtpclient
smtpClient.Send(message);
This will result in a single email being sent. All recipients will be able to see who the email went to. Depending on application, it might be a breach of privacy if sending to customers. Send
will block the calling thread.
Adding all emails to a single MailMessage and send asyncronously
var mailAddressCollection = new MailAddressCollection();
mailAddressCollection.Add("[email protected],[email protected]...");
// initialize the message and smtpclient
await smtpClient.SendMailAsync(message);
Same as above except SendMailAsync
will not block the calling thread.
Create a new MailMessage for each email address and send syncronously
// emailAddresses is a collection of strings
var mailAddressCollection = new MailAddressCollection();
foreach (var emailAddress in emailAddresses)
{
// initialize the message and smtpclient
smtpClient.Send(message);
}
This will result in one email being sent for each email address. The recipients will not know who else got the same email as they will be the only address if the "To" field. Because an individual email is being sent for each email address, the time to complete the loop is proportional to the number of email addresses. E.g. if it takes 2 seconds to send 1 email, it will take 200 seconds to send 100 emails.
Create a new MailMessage for each email address and send asyncronously
// emailAddresses is a collection of strings
var mailAddressCollection = new MailAddressCollection();
foreach (var emailAddress in emailAddresses)
{
// initialize the message and smtpclient
await smtpClient.SendMailAsync(message);
}
Same as above except SendMailAsync
will not block the calling thread.
A note on using SmtpClient
Microsoft do not recommend using SmtpClient
for new development as it doesn't support modern protocols. They currently recommend using MailKit or other libraries. Personally, I haven't used SmtpClient
in years, instead delegating all my email sending to third parties such as MailGun and SendGrid.
See https://github.com/dotnet/platform-compat/blob/master/docs/DE0005.md
Upvotes: 2
Reputation: 20373
Rather than serialise the email sending, you can dispatch the send requests in parallel then wait for them all to finish sending.
IEnumerable<MailMessage> emails = // create your email collection
var sendTasks = emails.Select(smtpClient.SendMailAsync);
await Task.WhenAll(sendTasks);
This will be more efficient when there is a large number of emails to send.
Upvotes: 2