【问题标题】:How to make SendGrid run without blocking?如何让 SendGrid 无阻塞地运行?
【发布时间】:2016-08-04 02:02:18
【问题描述】:

这是我的代码

public static void sendEmail(string sendto, string subject, string template, Object model)
{
    SendGridAPIClient sg = new SendGridAPIClient(ConfigurationManager.AppSettings["SendGridAPIKey"]);
    Email from = new Email(ConfigurationManager.AppSettings["EmailSender"]);
    Email to = new Email(sendto);
    var templateFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EmailTemplates");
    var tpl = File.OpenText(templateFolder + "\\" + template).ReadToEnd();
    string textbody = Engine.Razor.RunCompile(tpl, template, null, model);
    Content content = new Content("text/plain", textbody);
    Mail mail = new Mail(from, subject, to, content);
    dynamic response = sg.client.mail.send.post(requestBody: mail.Get());
}

这个函数在很多地方被调用。如何在不阻塞调用此函数的多个位置的情况下使此函数运行?

【问题讨论】:

    标签: asp.net-mvc sendgrid


    【解决方案1】:

    您可以使用HostingEnvironment.QueueBackgroundWorkItem

    using System.Web.Hosting;
    
    HostingEnvironment.QueueBackgroundWorkItem((ct) => sendEmail(sendto, subject, template, model));
    

    【讨论】:

    • 谢谢,虽然 API 返回看起来不错,但由于某种原因现在似乎没有收到电子邮件
    猜你喜欢
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 2021-06-14
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    相关资源
    最近更新 更多