Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC background threads for email creation and sending

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

like image 440
jrob Avatar asked Sep 03 '10 16:09

jrob


People also ask

How can we send mail asynchronously using background thread in asp net?

For sending email asynchronously in background using Thread, a new Thread object is defined and within which the SendEmail function is called using delegate in C# and Sub in VB.Net. Inside the SendEmail function all these parameters are set into an object of the MailMessage class.

How can you send an email message from an asp net web page?

First: Edit Your AppStart Page SmtpPort: The port the server will use to send SMTP transactions (emails). EnableSsl: True, if the server should use SSL (Secure Socket Layer) encryption. UserName: The name of the SMTP email account used to send the email. Password: The password of the SMTP email account.

What is hangfire in asp net core?

The Hangfire. AspNetCore integration package adds an extension method to register all the services, their implementation, as well as logging and a job activator. As a parameter, it takes an action that allows to configure Hangfire itself. Configuration settings below for new installations only.


1 Answers

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.

like image 92
Darin Dimitrov Avatar answered Sep 21 '22 20:09

Darin Dimitrov