views:

47

answers:

2

There are a few similar questions. I just want to clarify one part.

If I want to send an e-mail (up to 30) in response to user's action, what execution context do I do it from?

  1. Do I do a sync call (mail server is in the same data center, so actual sending is fast)?

  2. Do I spawn a thread and send it from there? such that each user-request-to-send mail gets handled by a new thread?

  3. Do I have a background thread that handles a queue of e-mails?

The biggest issue I think, is that it is a shared web hosting. So I don't know what's a good way to install/start a windows service, or start a dedicated thread.

Details: ASP.NET MVC app, hosted on IIS 7.0 integrated mode, on discountasp.net using discauntasp.net smtp server.

Is there a better way? What's a "standard" way to accomplish this?

+1  A: 

Shared hosting or not, your threads will only be able to use the resources alloted to you. If you are only shooting off a queue of 30 as a time, then you probably want to use a queue because spinning off 30 threads in such a situation would seem a little silly to me. It would probably take more processing time to create the threads and close them down than to send one e-mail to the mail server. (half joking)

Jeremy Petzold
He can queue all 30 emails on one thread which would be just fine.
Middletone
+1  A: 

Most datacenters have an smtp address that they can provide you in order to send mail. I'd just create the mail object and send it to that local smtp server. If you don't have access to one then you can have your smtp adapter connect to an external service and provide it's authentication information which should let you send mail through it. Non local addresses usually require this.

If you have to send a substantial amount of mail out or your server has a high load I suggest that you do either spawn a different thread or if you have access to the box that you add in a service that can pick up the items and send them out apart from your web application. This is because threads in your web app could be recycled or die and if you do end up with a long running process this could mean data loss or loss of functionality. By establishing a queue you can provide fast functionality to your users without compromising the QoS.

If as you suggest that this is a shared box then you will want to spawn a new thread to do this action for you. Reactive extensions can provide a more robust way for you to do this and is a part of the TPL (Task Parallel Library). Let me know if you need any more specific examples.

Middletone