7

I built a website for a client and they would like a custom newsletter tool. Building the tool was easy, but I'm not sure how to send the email.

I set up a test page and managed to send a test email to myself using the System.Net.Mail namespace. I tried applying this code to a loop on the newsletter page, but it's turning out to be quite a difficult task. The email sending loop locks up the whole site for about an hour while it sends its emails. Sometimes it will abort the loop midway and some of the emails won't get sent.

I tried starting the loop on another thread.

protected void btnSendNewsletter_Click(object sender, EventArgs e)
{
    Thread t = new System.Threading.Thread(new ThreadStart(SendEmails));
    t.Start();
}

but this still makes the site go slow and also has a habit of aborting part way through. What is the common method for sending mass emails? I'm sure I'm not doing it right.

I am very new to the email arena in .NET.

halfer
  • 19,824
  • 17
  • 99
  • 186
CatDadCode
  • 58,507
  • 61
  • 212
  • 318

1 Answers1

6

For this kind of task you're better to add a bunch of jobs to a queue. Then have a thread running which pulls x number of jobs from the queue, processes them (i.e. sends the emails) and then sleeps for a period of time. This will give you web app some breathing space.

If you're using a database you can create an Email Queue table to store the jobs. I prefer to use this kind of storage over memory incase the app recycles for some reason or an exception is thrown...atleast you can then pick up from where you left off.

Generally, the process running the worker thread wouldn't be the web app itself. It would be a windows service or something similar. This might not be possible if you're on shared hosting.

Lee Gunn
  • 8,417
  • 4
  • 38
  • 33
  • Very informative and succinct. Thank you. Do you know of any third party APIs you can sign up for to dump the load onto them and let them send out the emails? – CatDadCode May 05 '11 at 18:58
  • Agreed. While it won't be possible for Chevex to create a service (shared hosting), this method you specified will still work in the main system. – IAmTimCorey May 05 '11 at 19:00
  • I am thinking the cheapest option would be to let a 3rd party handle the email load. I just can't seem to find a good mass email api. Is there a popular one that most people use? – CatDadCode May 05 '11 at 19:04
  • @Chevex there are some 3rd party providers recommended in the answers to the question linked to above: http://stackoverflow.com/q/3905734/310112 – Joel C May 05 '11 at 19:10
  • 1
    In the past I've used MailChimp http://www.mailchimp.com/ but this involved my application creating the email template from a CMS, using tokens for name and salutation etc. The single tokenised template is then passed to MailChimp. MailChimp would then create all the emails from your user database by replacing all the tokens with the correct data and send them. You can either host your user list with mail chimp or I *think* they might offer an API to get at your remote database via some kind of proxy – Lee Gunn May 05 '11 at 19:11