异步发送电子邮件 [英] Send async e-mails
本文介绍了异步发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用ASP.NET MVC 3 MVCMailer,我尝试使用SendAsync发送电子邮件,但实际上它仍然需要更长的时间。
I am using ASP.NET MVC 3 with MVCMailer, I tried to send e-mails using SendAsync, but actually it still take longer.
所以我试图用Task.Factory像code波纹管:
So I am trying to use Task.Factory like the code bellow:
var task1 = Task.Factory.StartNew(
state =>
{
var mail = new UserMailer();
var msg = mail.Welcome("My Name", "myemail@gmail.com");
msg.SendAsync();
});
task1.Wait();
问题是,MVCMailer需要HttpContext的,但是这里面的任务,我的HttpContext空。
The problem is, MVCMailer needs HttpContext, but inside this task I got HttpContext Null.
我怎么能发送异步电子邮件?
How can I send Async e-mails?
推荐答案
一个小除了这一点。这里是一个扩展的方法来帮助一些。
A small addition to this. Here is an extension method to help some.
using Mvc.Mailer;
using System.Threading.Tasks;
public static void SendEmailAsync(this MvcMailMessage msg, HttpContext currContext)
{
//make this process a little cleaner
Task.Factory.StartNew(() =>
{
System.Web.HttpContext.Current = currContext;
msg.SendAsync();
});
}
使用它喜欢沿着从控制器的方法。
Use it like follows from your controller methods.
Mailers.UserMailer um = new Mailers.UserMailer();
um.SendWelcomeEmail(dataObject).SendEmailAsync(ControllerContext.HttpContext.ApplicationInstance.Context);
这篇关于异步发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文