In the blog I'm working on currently whenever an user posts a comment for an article I like to send emails to all the other users commented for that post. What is the best way in sending mails in this case either synchronously or asynchronously? Can anyone share a code snippet for sending email to a list of users using System.Net.Mail in .NET.
6 Answers
You might want to use a separate webservice for sending emails. You app will send the body and email list to the webservice. You can use gzip or zip compression. This would make the webservice call very efficient(70%+ compression as the data is text).
Now in the webservice you can use multi-threading or Async operations for sending emails.
Edit: If you have a dedicated server, making a windows service might be a better option. Make a two tables in sql server, Queued Emails and Send Emails(Archive). So whenever user posts a comment, update the Queued emails table. Now the windows service can wake up every 30 seconds, extract all the rows from Queued emails and async send all of them. After an email is send, move/remove the row from the table.

- 21,087
- 11
- 87
- 112
-
That's interesting. Never thought about that. – LeftyX May 31 '11 at 08:52
-
So? The webservice can send separate emails to all the addresses. – A G May 31 '11 at 09:01
-
In a single thread we can send all the emails or for every email a separate thread in the service? – VJAI May 31 '11 at 09:08
-
@LeftX: OP removed his comment. I was replying to him. – A G May 31 '11 at 09:09
-
@Aseem i removed my comment bcoz you edited yours. I try to keep things as simple as possible and looking for solutions that won't add more maintenance and overhead on the road. I think that maintaining web service is easier than the windows service and planning to go with that. – VJAI May 31 '11 at 09:14
-
@Vijaya Anand: make up your mind which one is gonna be the best answer ;-) – LeftyX May 31 '11 at 09:21
I posted another StackOverflow answer addressing this issue of sending lots emails which can find by clicking here.
It is of course better to send email asynchronously to be able to maximize on how many can be sent in a fixed time frame. You can do this yourself or use already written components to do it.
I use Fluent.NET Mail to construct and send single emails and I use MassMailer.NET to send large volumes of emails.
Fluent.NET Mail
new Mail()
.Html(new MessageTemplate()
.FromText("This is my email with {content} text")
.DataFrom(new { content = "html" })
.BetweenBraces())
.To(new MailBox(emailAddress))
.From(new MailBox(emailAddress))
.Subject("Fluent API .NET 2")
.Timeout(5000)
.SmtpServer("[your smtp server]")
.SendAsync();
MassMailer.NET
Check out this post for an example on how to send a large number of emails.
Have a look at this
Sending Email with System.Net.Mail
MailMessage message = new MailMessage();
message.From = new MailAddress("sender@foo.bar.com");
message.To.Add(new MailAddress("recipient1@foo.bar.com"));
message.To.Add(new MailAddress("recipient2@foo.bar.com"));
message.To.Add(new MailAddress("recipient3@foo.bar.com"));
message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
message.Subject = "This is my subject";
message.Body = "This is the content";
SmtpClient client = new SmtpClient();
client.Send(message);

- 41,517
- 32
- 98
- 131
-
If I use above approach (I haven't tried yet!) I fear that a single user can see other's email addresses that's not good, correct me if I'm wrong. – VJAI May 31 '11 at 08:55
-
Have a look at this thread: http://stackoverflow.com/questions/2651625/send-multiple-emails – Naveed May 31 '11 at 08:59
MailMessage message = new MailMessage();
message.From = new MailAddress("sender@foo.bar.com");
message.To.Add(new MailAddress("recipient1@foo.bar.com"));
message.To.Add(new MailAddress("recipient2@foo.bar.com"));
message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
message.Subject = "This is my subject";
message.Body = "This is the content";
SmtpClient client = new SmtpClient();
client.Send(message);
You can find more info here or here if you need to use NetworkCredential
for authentication.
Sending it synchronously? Well, it depends how many you have to send. It can be time consuming.
In the situation where I have to send bulk emails and I do not want to hung my web server I develop a custom windows services which scans the DB periodically, collects info and sends emails.

- 35,328
- 21
- 132
- 193
-
Creating a custom windows service for sending emails sounds good. I'll think about that. – VJAI May 31 '11 at 08:57
-
@Vijaya Anand: It depends how many emails are involved. It adds more complexity to the infrastructure but, surely, you won't have problems in the future. You might even consider @Aseem Gautam solutions, which seems interesting. – LeftyX May 31 '11 at 09:03
I use this to send mails asynchronously
SmtpClient smtp = new SmtpClient();
foreach(var mail in mailsToSend)
{
new Thread(() =>
{
smtp.Send(mail);
}
).Start();
}

- 9,800
- 16
- 53
- 74
-
In the above code you are creating a new thread for every email, though I'm not a pro in async programming but concerned about the no. of threads being created for sending emails. – VJAI May 31 '11 at 09:04
If you are considering a third-party service, there is a decent article on getting started with Amazon's SES Email using .NET.
This worked very well for me, but note that SendGrid just announced new pricing equal to the Amazon's SES, SendGrid includes an SMTP wrapper so the code is even simpler.

- 5,167
- 3
- 24
- 34