【问题标题】:How to set up identity emails for SendGrid for ASP.NET 4.6.1如何为 ASP.NET 4.6.1 的 SendGrid 设置身份电子邮件
【发布时间】:2018-07-31 02:57:56
【问题描述】:

SendGrid 建议使用其版本 6.3,但仅支持 .NET 4.5;我的应用程序是 4.6.1,并在 Azure 应用程序服务上提供服务。

我想尝试通过 SendGrid 发送 smtp 并仅查看 this page 的文档。它没有显示如何在 IdentityConfig 类中编写消息,也没有说明如何/在何处通过 Azure 的环境变量存储 https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/csharp.html#-Using-NETs-Builtin-SMTP-Library 引用 SendGrid apikey。

非常感谢您对此的帮助。

【问题讨论】:

  • SendGrid 的最新版本似乎是 9.9, in nuget。你在哪里看到 6.3?
  • 我在使用 9.9 时遇到了错误,我用谷歌搜索并被建议转到 6,3,这是我通过使用 install 命令指定版本从 Nuget 获得的。
  • 抱歉,如果这个问题的标题令人困惑。我在运行 v3 代码时遇到了麻烦,并看到一个自述文件说最新的稳定版本是 6.3 。我无法使 6.3 工作,所以回到最新版本,如下面的答案所示。我必须提醒的是,至少在我的情况下,代码在 localhost 上静默失败,但在生产中有效。

标签: sendgrid asp.net-4.6


【解决方案1】:

我再次查看了 SendGrid 文档,发现 2017 年末的更新并没有说 api 仅针对 4.5.*,因此决定尝试最新版本 9.8 。

它与开箱即用的 Register Post 方法一起使用,只需将重定向添加到“确认发送”视图。

这是适合我的代码:

        public Task SendAsync(IdentityMessage message)
    {

        return configSendGridasync(message);
    }

    private async Task configSendGridasync(IdentityMessage message)
    {
   var apiKey = System.Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
       var client = new SendGridClient(apiKey);

       var msg = new SendGridMessage();

      msg.AddTo(message.Destination);
      msg.From = new EmailAddress("info@XXX.org", "Website Name");
     msg.Subject = message.Subject;
      msg.PlainTextContent = message.Body;
     msg.HtmlContent = message.Body;
        var response = await client.SendEmailAsync(msg);
    }
}

【讨论】:

    猜你喜欢
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多