ASP.Net MVC background threads for email creation and sending

后端 未结 1 1497
醉梦人生
醉梦人生 2020-12-24 03:53

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

相关标签:
1条回答
  • 2020-12-24 04:38

    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.

    0 讨论(0)
提交回复
热议问题