Mic*_*ade 10 c# asp.net-mvc mvcmailer
我正在使用ASP.NET MVC 3和MVCMailer,我尝试使用SendAsync发送电子邮件,但实际上它还需要更长的时间.
所以我试图像下面的代码一样使用Task.Factory:
var task1 = Task.Factory.StartNew(
state =>
{
var mail = new UserMailer();
var msg = mail.Welcome("My Name", "myemail@gmail.com");
msg.SendAsync();
});
task1.Wait();
Run Code Online (Sandbox Code Playgroud)
问题是,MVCMailer需要HttpContext,但在这个任务中我得到了HttpContext Null.
如何发送Async电子邮件?
Mat*_*att 19
这是一个小小的补充.这是一个帮助一些的扩展方法.
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();
});
}
Run Code Online (Sandbox Code Playgroud)
从控制器方法中使用它如下所示.
Mailers.UserMailer um = new Mailers.UserMailer();
um.SendWelcomeEmail(dataObject).SendEmailAsync(ControllerContext.HttpContext.ApplicationInstance.Context);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5298 次 |
| 最近记录: |