【问题标题】:SendGrid not working from Azure App ServiceSendGrid 在 Azure 应用服务中不起作用
【发布时间】:2016-12-27 10:59:46
【问题描述】:

我正在使用最新的 SendGrid .NET 包 (8.0.3) 从我的 ASP.NET Core Web 应用程序发送电子邮件:

public Task SendEmailAsync(string email, string subject, string message)
{
    return Send(email, subject, message);

}

async Task Send(string email, string subject, string message)
{
    dynamic sg = new SendGridAPIClient(_apiKey);

    var from = new Email("noreply@my.domain", "My Name");
    var to = new Email(email);
    var content = new Content("text/html", message);
    var mail = new Mail(from, subject, to, content);

    await sg.client.mail.send.post(requestBody: mail.Get());
}

它在本地工作,但在 Azure 应用服务实例上运行,邮件无法通过。

代码运行良好,没有任何异常,所以我几乎没有什么可使用的,但我认为这可能是某种防火墙问题。

有没有人遇到过类似的问题?我该如何调试呢?

【问题讨论】:

  • 您的 SendGrid 帐户是否有任何 IP 限制? sendgrid.com/docs/User_Guide/Settings/ip_access_management.html
  • 不,我没有 IP 限制。查看尝试访问的列表,Azure 的最后一次尝试似乎是在三个月前,因此很明显,调用 SendGrid 的尝试在两者之间的某个地方被阻止了。

标签: c# azure sendgrid asp.net-core-1.0


【解决方案1】:

虽然这个问题很老,但我提供了一个答案,因为我遇到了类似的问题并且找不到任何明确的解决方案。它对我不起作用的原因是因为

  • 我没有引用 Microsoft.Azure.WebJobs.Extensions.SendGrid。解决方案是从 Nuget 添加它
  • 我没有等待异步发送完成。将 .Wait() 添加到发送电子邮件的异步函数中。我用于的完整代码 program.cs 如下:

    static void Main()
    {
        var config = new JobHostConfiguration();
    
        if (config.IsDevelopment)
        {
            config.UseDevelopmentSettings();
        }
        config.UseSendGrid();
    
    //The function below has to wait in order for the email to be sent
        SendEmail(*Your SendGrid API key*).Wait();
    
    
    }
    
    public static async Task SendEmail(string key)
    {
    
        dynamic sg = new SendGridAPIClient(key);
    
        Email from = new Email("from@domain.com");
        string subject = "Test";
        Email to = new Email("to@domain.com");
    
        Content content = new Content("text/html", "CONTENT HERE");
        Mail mail = new Mail(from, subject, to, content);
    
    
    
        dynamic s = await sg.client.mail.send.post(requestBody: mail.Get());
    
    }
    

所以我建议你将 .Wait() 添加到你的函数调用中:

public Task SendEmailAsync(string email, string subject, string message)
{
    //Added .Wait()
    return Send(email, subject, message).Wait();
}

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,对我来说问题是我将 SendGrid apiKey 作为环境变量存储在项目中,它可以在本地主机上运行,​​但一旦 api 托管在 azure 上就没有了。

    【讨论】:

    • 那么它应该在 Azure 上的什么位置?我在 Azure 应用程序设置上创建了该设置,但它仍然对我不起作用。
    【解决方案3】:

    我也在 Azure Web App 中使用 SendGrid。我可以在本地调试和部署到 Azure 中的生产后发送。这里有几件事需要检查:

    1. API 密钥 - API 密钥显然是必需的;仔细检查你是如何填充它的。通常这个值是通过配置设置的。您部署的配置值是否与本地运行时的值相同?也许您在部署过程中遇到了 web.config 转换,或者您应该设置一个覆盖设置(例如,如果您使用的是 Web 应用服务,请在应用的应用程序设置中)。
    2. SendGrid 活动和抑制 - 您是否检查过 SendGrid 的活动日志?问题可能是将您的请求发送到 SendGrid,或者可能是 SendGrid 正在获取您的请求但没有满足它。检查活动提要并搜索您期望的特定电子邮件(日志可搜索并按时间排序)。如果您在此处看到与您的电子邮件相对应的日志,则问题出在 SendGrid 方面。有时电子邮件被禁止,并且可能由于多种原因而发生(退回邮件、标记为垃圾邮件,甚至您的 DNS 服务器无法用于域所有权验证)。如果是这种情况,日志将提供证据。您也可以直接转到 Suppressions 并在那里搜索电子邮件地址。
    3. 网络可用性 - 如果您使用 Web App 服务托管您的应用程序(看起来就是这种情况),那么您可以假设您的 Web 应用程序可以发出网络请求,这显然是命中 SendGrid API 或 SMTP 所必需的服务器。但是,如果您将应用程序部署到托管在 Azure 中的 VM,那么可能会遇到一些问题,例如防火墙。登录虚拟机并手动确认您可以直接向 SendGrid API 发出网络请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多