ASP.Net MVC后台线程用于电子邮件创建和发送 [英] ASP.Net MVC background threads for email creation and sending

查看:144
本文介绍了ASP.Net MVC后台线程用于电子邮件创建和发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在看postmarkapp.com来处理从我们的asp.net mvc 2应用程序发送电子邮件,使用他们提供的.net库:postmark-dotnet library



在他们的文档中,他们提到发送带有附件的电子邮件可能需要一些时间,在后台工作中做得更好。对于我的应用程序,我可以发送任何地方从10到500个个性化电子邮件,一些附件批量给我的用户。




  • 什么是以非阻塞的方式对在ASP.Net MVC中启动创建和发送这些电子邮件的管理员用户进行此后台处理的最佳方法


  • 如果他们点击创建和发送电子邮件到500个用户,并在完成该过程之前关闭浏览器会怎么样?




感谢任何帮助!新的ASP.Net MVC

解决方案

你可以生成一个新的线程,并发送这个线程的邮件:

  [HttpPost] 
[授权(Roles =管理员)]
public ActionResult SendMails()
{
new Thread(()=>
{
//在这里发送电子邮件
})。
return View();
}

如果用户关闭浏览器,此线程将继续运行直到完成或AppDomain关闭。该操作将立即返回一个视图,它不会被阻止。



同样,将数据库中的一些标志设置为发送电子邮件的操作正在运行,以便如果管理员在按钮上点击两次您的用户不要用很多邮件敲打。



如果你想要更强大的解决方案,你可以看一下这里是一个教程,可以让您快速入门。


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

解决方案

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.

这篇关于ASP.Net MVC后台线程用于电子邮件创建和发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆