10

我正在使用带有 MVCMailer 的 ASP.NET MVC 3,我尝试使用 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();

问题是,MVCMailer 需要 HttpContext,但在这个任务中我得到了 HttpContext Null。

如何发送异步电子邮件?

4

4 回答 4

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();
       });
}

从您的控制器方法中使用它,如下所示。

Mailers.UserMailer um = new Mailers.UserMailer();
um.SendWelcomeEmail(dataObject).SendEmailAsync(ControllerContext.HttpContext.ApplicationInstance.Context);
于 2013-03-03T04:07:00.213 回答
4

Task.Factory.StartNew将创建一个新线程。
如果要访问主线程中的HttpContext,则必须执行以下操作:

var task1 = Task.Factory.StartNew(() =>
    {
    System.Web.HttpContext.Current = ControllerContext.HttpContext.ApplicationInstance.Context;
    var mail = new UserMailer();
    var msg = mail.Welcome("My Name", "myemail@gmail.com");
    msg.SendAsync();
    });

task1.Wait();

使用 TPL 或 QueueUserWorkItem 是否更好存在长期争论。
有人试图解决这个问题
这是 QueueUserWorkItem 版本:

public class HomeController : Controller
{
    private AutoResetEvent s_reset = new AutoResetEvent(false);

    public ActionResult Index()
    {
        var state = new WorkerState() { HttpContextReference = System.Web.HttpContext.Current };
        ThreadPool.QueueUserWorkItem(new WaitCallback(EmaiSenderWorker), state);

        try
        {
        s_reset.WaitOne();
        }
        finally
        {
        s_reset.Close();
        }

        return View();
    }

    void EmaiSenderWorker(object state)
    {
        var mystate = state as WorkerState;

        if (mystate != null && mystate.HttpContextReference != null)
        {
        System.Web.HttpContext.Current = mystate.HttpContextReference;
        }

        var mail = new UserMailer();
        var msg = mail.Welcome();
        msg.SendAsync();

        s_reset.Set();
    }

    private class WorkerState
    {
        public HttpContext HttpContextReference { get; set; }
    }

}
于 2012-08-30T21:44:26.560 回答
0
public class UserMailer : MailerBase    
{
    RegisterService Service = new RegisterService();
    HttpContext Context;
    public UserMailer(HttpContext context)
    {
        Context = context;
        MasterName="_Layout";
    }

    public void ConfirmRegistration(Register model)
    {
        SendAsync(() =>
        {
            model.Conference = Service.Context.Conferences.Find(model.ConferenceID); // load conference bcs it fails to lazy load automatically.
            ViewData.Model = model;
            return Populate(x =>
            {
                x.Subject = "You registered for " + model.Conference.Name + "!"; ;
                x.ViewName = "Confirm";
                x.To.Add(model.Email);
            });
        });
    }

    private void SendAsync(Func<MvcMailMessage> GetEmail)
    {
        Task.Factory.StartNew(() =>
        {
            System.Web.HttpContext.Current = Context;
            GetEmail().Send();
        });
    }
于 2012-10-06T09:40:45.917 回答
0

你不需要任务。SendAsync 是异步的并使用另一个线程自身。任务不会加速你的邮件。

更新:当我解决同样的问题时,我使用任务和同步发送。看来 SendAsync 不是那么异步。这是我的代码示例(它不需要 HttpContext):

public void SendMailCollection(IEnumerable<Tuple<string, string, MailAddress>> mailParams)
    {
        var smtpClient = new SmtpClient
        {
            Credentials = new NetworkCredential(_configurationService.SmtpUser, _configurationService.SmtpPassword),
            Host = _configurationService.SmtpHost,
            Port = _configurationService.SmtpPort.Value
        };

        var task = new Task(() =>
                                {
                                    foreach (MailMessage message in mailParams.Select(FormMessage))
                                    {
                                        smtpClient.Send(message);
                                    }

                                });
        task.Start();
    }

    private MailMessage FormMessage(Tuple<string, string, MailAddress> firstMail)
    {
        var message = new MailMessage
            {
                From = new MailAddress(_configurationService.SmtpSenderEmail, _configurationService.SmtpSenderName),
                Subject = firstMail.Item1,
                Body = firstMail.Item2
            };
        message.To.Add(firstMail.Item3);
        return message;
    }
于 2012-08-30T16:51:10.027 回答