RPM*_*984 11 actionmailer html-email razor mvcmailer asp.net-mvc-3
我们从ASP.NET MVC 3 Razor Web应用程序发送电子邮件.
目前我们正在使用ActionMailer.NET.
我看过MvcMailer.
两者的问题是他们需要一个Http Context来执行.
这个问题是我想要异步发送电子邮件.现在我知道您可以异步执行实际发送(例如SMTP调用),但我希望发送电子邮件的整个过程是异步的,例如:
public ActionResult DoSomething(Something something)
{
_db.Save(something);
Task.Factory.StartNew(() => {
new MailController().DoSomething().Send(something);
});
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
在"DoSomething"方法中,我再次查询数据库,执行其他操作等等....我希望所有这些都是异步的 - 因此整个调用都包含在一个任务中,而不是仅仅执行.SendAsync().希望有道理.
上面的示例是ActionMailer,它会中断 - 因为HTTP上下文在生成的线程中消失了.
有谁知道我怎么能让这个工作,或者另一个不依赖于HTTP上下文存在的包?
我不确定为什么需要HTTP上下文 - 这里没有请求路由,只需将文件系统上的视图解析为HTML.
您可以查看使用 RazorViewEngine 的邮政:
public class HomeController : Controller
{
public ActionResult Index()
{
Task.Factory.StartNew(() =>
{
Thread.Sleep(5000);
dynamic email = new Email("Example");
email.To = "webninja@example.com";
email.FunnyLink = "some funny link";
email.Send();
});
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
和里面~/Views/Emails/Example.cshtml:
To: @ViewBag.To
From: lolcats@website.com
Subject: Important Message
Hello,
You wanted important web links right?
Check out this: @ViewBag.FunnyLink
Run Code Online (Sandbox Code Playgroud)
在将应用程序投入生产之前,还要确保您已阅读并理解在 ASP.NET 应用程序中实现后台任务的危险。
| 归档时间: |
|
| 查看次数: |
2358 次 |
| 最近记录: |