tags:

views:

201

answers:

4

I'm having an issue sending large volumes of emails out from an ASP.Net application. I won't post the code, but instead explain what's going on. The code should send emails to 4000 recipients but seems to stall at 385/387.


The code creates the content for the email in a string.

It then selects a list of email address to send to.

Looping through the data via a datareader it picks out the email address and sends an email.

The email sending is done by a separate method which can handle failures and returns it's outcome.

As each record is sent I produce an XML node in an XML document to log each specific attempt to send.

The loop seems to end prematurely and the XML document is saved to disk.


Now I know the code works. I have run it locally using the same SMTP machine and it worked fine with 500 records. Granted there was less content, but I can't see how that would make any difference.

I don't think the page itself times out, but even if it did, I was sure .Net would continue processing the page, even if the user saw a page time out error.

Any suggestions appreciate because I'm pretty stumped.

A: 

If the web server has a timeout setting, it will kill the page if it runs too long.

Anon.
+4  A: 

You're sending lots of emails. During the span of a single request? IIS will kill a request if it takes longer than a certain (configurable) amount of time.

You need to use a separate process to do stuff like this. Whether that's a Timer you start from within global.asax, or a Thread which checks for a list of emails in a database/app_data directory, or a service you send a request to via WCF, or some combination of these.

Will
Sending large amounts of email from within asp.net, no matter how you are doing it (timers or what not) is a bad idea. Use a separate service app to handle the email stuff.
Stephen M. Redd
+2  A: 

The way I've handled this in the past is to queue the emails into a SQL Server table and then launch another thread to actually process/send the emails. Another aspx utility page can give me the status of the queue or restart the processing.

I also highly recommend that use an existing, legit, third-party mailing service for your SMTP server if you are sending mail out to the general public. Otherwise you run the risk of your ISP shutting off your mail access or (worse) your own server being blacklisted.

Bryan
A: 

I recommend you check the value of HttpServerUtility.ScriptTimeout - if this is set then when a script has run for that length of time, it will be shut down.

Something you could do to help is go completely old-school - combine some Response.Writes with a few Response.Flush to send some data back to the client browser, and this tends to keep the script alive (certainly worked on an old ASP.NET 1.1 site we had).

Also, you need to take into account when this script is being run - the server may well also have been configured to perform an application reset (by default this is set to every 29 hours in IIS), if your server is set to something like 24 hours and this coincides with the time your script it run, you could be seeing that too - although the fact that the script's logging its response probably rules that out - unless your XML document is badly formed?

All that being said, I'd go with Will's answer of using a seperate process (not just a thread hosted by the site), or as Bryan said, go with a proper mailing service, which will help you with things like bounce backs, click tracking, reporting, open counts, etc, etc.

Zhaph - Ben Duguid