views:

122

answers:

2

I'm looking at postmarkapp.com to handle the sending of email from my asp.net mvc 2 application using the .net library that they provide: postmark-dotnet library

In their documentation they mention that sending emails w/ attachments can take some time and its better to do this in a background job. For my application I could be sending anywhere from 10 to 500 personalized emails some with attachments in a batch to my users.

  • What is the best way to do this background processing in a non blocking way to the admin user that initiated the creation and sending of these emails in ASP.Net MVC?

  • What happens if they click "create and send emails" to 500 users and close out the browser before that process is complete?

Thanks for any help! New to ASP.Net MVC

+2  A: 

You could spawn a new thread and send the mails in this thread:

[HttpPost]
[Authorize(Roles = "Administrator")]
public ActionResult SendMails()
{
    new Thread(() => 
    {
        // Send the emails here
    }).Start();
    return View();
}

If the user closes the browser this thread will continue running until it completes or the AppDomain shuts down. The action will return a view immediately and it won't block.

Also it could be a good idea to set some flag into the database that an operation of sending emails is running so that if the administrator clicks twice on the button your users don't get hammered with lots of mails.

If you want more robust solution you could take a look at MSMQ. And here's a tutorial that should get you started quickly.

Darin Dimitrov
Task > Thread (if .NET 4)
Necros
+1  A: 

Consider another option: writing a web service to send emails for you. Keep the email process out of your app. The app can simply fire-and-forget over to this web service.

Here's some sample code from CodeProject: http://www.codeproject.com/KB/aspnet/XYEMailService.aspx It's vintage 2004 code. Perhaps it can be leveraged as a new WCF web service.

This lends itself to being called from all your apps. No need to keep writing/including/referencing the same assembly in all your many apps. Just deploy and configure once, and add the reference in each project.

p.campbell