1

We are interested in implementing something into our ASP.NET web application that will send out an e-mail to addresses pulled from SQL Server 2008.

This doesn't seem like it would be that difficult. Pseudo-code:

EmailAddressList addresses = EmailAddressManager.GetList();

foreach (EmailAddress x in addresses)
{
    MailMessage msg = new MailMessage("fromaddress@test.com", x.ToAddress);
    //prepare msg, body, subject, etc.

    SmtpClient smtp = new SmtpClient();
    smtp.Send(msg);

    //add artificial delay for throttling?  this seems to be a common tactic
}

This question has an upvoted comment that links to something that was supposedly helpful, but now just leads to a Page Not Found error.

And in this question, the OP says specifically that the above pseudo-code method isn't "the good way." But what is wrong with it? Is there a common or better practice for this? Are there hidden pitfalls I'm not taking into account?

And just a little bit more detail about our situation: this will only be used maybe twice per month, and the number of emails to be sent at once will not be above roughly 30 or 40. The end goal is, we go to this page that has this tool, we can preview what's going to get sent out and to who, and then we just press a button and it executes the above pseudo-code.

Community
  • 1
  • 1
CptSupermrkt
  • 6,844
  • 12
  • 56
  • 87
  • 2
    Maybe an ASP.NET page isn't the best way to do this. What if the user presses "F5" - your code will run again. – John Saunders Oct 17 '12 at 01:47
  • Most ISP's will block emails after 500 or so are sent from one IP in a short amount of time. It's a security measure to keep spyware from hijacking someones PC and sending emails. You might research using a mass email service instead. – Scottie Oct 17 '12 at 01:56
  • @John Saunders Thanks for the reply. That's a good point. What if we added a Redirect("someotherpage.aspx") at the end of the "Send" button click event? – CptSupermrkt Oct 17 '12 at 01:58
  • 1
    What if you don't use ASP.NET pages for things other than interacting with a user? You need a service of some kind. A Windows Service is easy to create, and it can host a simple WCF service that can be used by the web application to queue work to the service. – John Saunders Oct 17 '12 at 01:59
  • I think I see what you mean. I'll have to study up on Windows and WCF services. We have an intranet website that is basically one giant toolbox for doing company stuff --- user logs in, administration can view/modify employee vacations, IT can check out and refresh the ESX server list page to see which virtual machines are on what ESX server, and so on. We actually already have a mass mailer application as a winform, but the idea now is to put that functionality into the intranet site so that we can get closer to our goal of having all these tools in one place. – CptSupermrkt Oct 17 '12 at 02:17

1 Answers1

0

I don't think there's anything wrong with a loop like you're doing, but there are a few things you probably need to handle. The most important would probably be error handling. If one of the emails fails, you'll just break out having sent emails to a subset of people, and it'll be tough to deal with should you need to send to the rest. Even if you have some simple try-catch, you'll still have a problem retrying those emails. You'll want to have some way of storing all the emails you planned on sending to and the results of each send.

Next will be performance-related. Sending emails one at a time using .Send() is going to be slow if it's a lot of emails. An easy way around it is to use the Task library or the Async version to multithread the sends. Of course this makes the state management a little more complicated, but it will improve email sending speed significantly. If you have a limit to the number of emails you can send per minute/hour, then that may or may not be at odds with this optimization.

If you have different content for each recipient you'll probably want to use some templating library like NVelocity or the MS Razor Engine.

Or you could just outsource this boring stuff to somewhere like SendGrid.

Michael Yoon
  • 1,606
  • 11
  • 9