views:

48

answers:

2

I have a asp.net program, and I need to send a lot of emails. when i call http://localhost:70/sendemails system will send the emails one by one in seperated threads(just like async). I don't know if this is the best method. but what i want to know is: I schedule to send 10000 emails, and after i call the link, and then close the browser(means that the session will be closed too), Then if the threads i created will also be terminated ?

What is the best method to send lots of emails ?

+2  A: 

I would suggest that the ASP.NET application writes the information into a queue of some description - whether that's a message queue or just a table in the database.

Then have a separate service running to process the queue and send the emails. That way you don't need to wait until the emails have been sent before you respond, but you can still be sure that by the time the page responds, the request has been persisted.

Another alternative would just be to start a new thread to do the email sending within the ASP.NET application, but that means that if the application were to fall over (or be recycled) the request would be lost. It's easier to put the persistence and fault tolerance in a separate service, IMO.

Jon Skeet
Yeap- something like MSMQ would do.
RichardOD
A: 

The thread shouldn't be terminated by the closing of the browser. What could terminate the thread would be a recycle of the application (if memory grows too much, or other specific conditions arise).

The best method for mail sending would be to have a separate windows service, but if that's not possible then using a thread might be a workable idea, providing that you have a mechanism to restart the thread on an application recycle and restart the sending from where it was left. The problem with the restart is that you need a request to a page to get the thread back - you could do this from a computer you control using a scheduled task for example. Could work, but not very reliable compared to a windows service.

rslite