What is the best way to send large batches of emails in ASP.NET?
What is the best way to send large batches of emails in ASP.NET?
Question
I'm currently looping through a datareader and calling the System.Net.Mail.SmtpClient's Send() method. The problem with this is that it's slow. Each email takes about 5-10 seconds to send (it's possible this is just an issue with my host). I had to override the executionTimeout default in my web.config file (it defaults to 90 seconds) like this:
<httpRuntime executionTimeout="3000" />
One caveat: I'm on a shared host, so I don't think it is possible for me to send using the PickupDirectoryFromIis option (at least, it gave me errors when I turned it on).
Accepted Answer
You could send the mail asynchronous. That way the timeout should not interrupt your sending.
This article should help you get started with that: Sending Emails Asynchronously in C#.
There is another approach here: http://www.vikramlakhotia.com/Sending_Email_asynchronously_in_AspNet_20.aspx
And off course there are several commercial clients available, but the only one that i have tried and can recommend is http://www.aspnetemail.com/
Read more… Read less…
Definitely spawn it off on a background worker process so they go out asynchronously.
BTW, 5-10 seconds per e-mail seems way slow to me. On my server it takes just fractions of a second per e-mail.