views:

305

answers:

3

My application is a asp.net 3.5 running on iis 6 (windows 2003) This application is serving 1000's of users daily (100-500 users online).

I want to send an email newsletter to customers weekly.

Around 200,000 emails every time.

This is the code im using:

 ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncProcessMailerQueue), null);

 private static void AsyncProcessMailerQueue(object data)
 {
    for (int i=0;i<users.count ; i++)
    {
      MailMessage message = new MailMessage();
      .......
      SmtpClient smtpClient = new SmtpClient();
      smtpClient.Send(message);
    }
 }

When testing this locally (on my dev machine) i see the application is working alot slower.

  1. Is there a better way to write this code?
  2. Should i use ThreadPool.QueueUserWorkItem or create a new thread using Thread t = new Thread(new ThreadStart(DoWork)); ?
  3. Will it be better to create a totally seperate application for the purpose of sending the newsletters. will that help if ill run this application on the same machine?

i've seen other posts here talking about ThreadPool vs Thread but its seem no one is sure which is better.

Any information is welcome! Thank You!

+1  A: 

In order of preference:

  1. Create another application. Windows service would be a good choice
  2. Use Thread t = new Thread(new ThreadStart(DoWork));
  3. Your current implementation
Darin Dimitrov
thanks! , if i create a windows service , should i use threads in that as well ?if i keep using asp.net , why is new thread better than using ThreadPool?
sharru
Yes, using threads is recommended. If you keep using ASP.NET then using ThreadPool for long running tasks is a bad idea because these threads are used to service requests and if you have many of them blocked for such operations the site might stop working.
Darin Dimitrov
+2  A: 

Moving out of asp.net would be a good choice. This could be a simple command line app you run from a command prompt. Why do you need a service or it to be hosted as a URL?

No Refunds No Returns
i dont need it to be hosted as a url , but its just simpler as i already have all the function/data access code in this asp.net project.
sharru
@sharru, you can break up the project into different parts and all the function access and data stuff can be separated into a shared project. This is good design anyways, even if you didn't have this separate process to run.
Sam
its abit late for that :) its a big project. i can exact related code and use it , or i can just use the asp.net dll in the service , no?
sharru
You could consider an ashx file to get a lot of the junk you don't want out of the process. You're already inside of IIS and you've moved your work off to another thread. Might want to put something to prevent 2 or more from running concurrently if it's a load on your server(s).
No Refunds No Returns
- No Refunds No Returns , didn't really understand you comment , what would the ashx handler do?
sharru
A: 

Yes I think you should move this out of your web app, as you're taking up threads from the thread pool that are needed to serve your requests (sounds like you get a good amount of traffic).

You're also sending the emails syncronously which means the thread is being used for a lot longer than it need be - if you continue using this ThreadPool approach I would suggest queuing them to the IIS SMTP service (look at System.Net.Mail.SmtpClient.DeliveryMethod) which just writes a file to a queue folder, which is monitored by the IIS SMTP service.

But really you should consider moving this to a windows service.

JonoW
Thanks! , that sounds like a good advise!I guess this is not the default behavior of the SmtpClient ?
sharru
No problem. No its not the default behaviour, to do it you need to set mySmtpClientInstance.DeliveryMethod = DeliveryMethod .PickupDirectoryFromIis and ensure that the IIS SMTP service is running. Good luck..
JonoW