【问题标题】:sending mails using sendgrid from azure app-service webjob使用来自 azure app-service webjob 的 sendgrid 发送邮件
【发布时间】:2020-01-06 19:25:00
【问题描述】:

我开发了一个 C# 控制台应用程序,在某些情况发生时向我们的客户发送电子邮件。将应用程序部署为 azure webjob 时不发送邮件。

在我的开发环境中从 Visual Studio 进行测试时,邮件按预期发送而没有任何问题,但当应用程序部署并作为 azure app-service webjob 运行时,不会发送邮件。当部署为 azure webjob 时,该应用程序也可以按预期工作。

public enum MailType : short { Plain, Html }

private static string MimeType(MailType type) 
{ return (type == MailType.Html) ? "text/html" : "text/plain";  }

private static async Task sendMail(string to, string name, string subject, MailType mailType, string content)
{
    SendGridMessage msg = new SendGridMessage();
    msg.From = new EmailAddress("noreply@foo.org", "Foo Ltd.");
    msg.AddTo(to, name);
    msg.Subject = subject;
    msg.AddContent(MimeType(mailType), content);
    SendGridClient client = new SendGridClient(SendGridAPIKey);
    await client.SendEmailAsync(msg);
}

public static void SendMail(string to, string name, string subject, MailType mailType, string content)
{
    sendMail(to, name, subject, mailType, content).Wait();
}

关于这个问题的所有其他问题的答案(很多开发人员都遇到过这个问题)集中在代码上;特别是异步实现,但似乎这不是问题。 azure 对部署为 webjob 的应用程序和 sendgrid 服务器之间的通信是否有一些限制? (我正在使用 sendgrid 和 sendgrid.helpers.mail)

【问题讨论】:

  • 当你将webjob发布到azure时,你有没有在azure webapp的Configuration中设置SendGridAPIKey
  • 检查 SendGridAPIKey 发现了一个错误,但修复它并没有解决问题。部署为 azure webjob 时来自应用程序的请求未记录在 SendGrid 活动列表中(但来自我的开发环境的请求)
  • 你有没有解决过这个问题,如果有,解决方案是什么?
  • 我没有解决 SendGrid 问题,而是使用 MailJet

标签: azure sendgrid sendgrid-api-v3


【解决方案1】:

我用你的代码进行了测试,它在本地和 Azure 上都运行良好。我在 queueTrigger 中调用 SendMail 方法。

public static void ProcessQueueMessage([QueueTrigger("queue1")] string message, TextWriter log)
{
    log.WriteLine(message);
    SendMail("xxxxx@gmail.com", "xxxxx", "hello world", MailType.Plain, "have a good day.");
}

所以你可以参考下面的方法来解决它。

1.检查您的SendGridAPIKey。如果你将SendGridAPIKey作为环境变量存储在项目中,你需要到Configuration>Application Settings里面添加key。

2.检查您的 SendGrid 活动。问题可能是您的请求发送到 SendGrid,或者可能是 SendGrid 正在获取您的请求但没有满足它。检查活动提要并搜索您期望的特定电子邮件

【讨论】:

  • 检查 SendGridAPIKey 发现了一个错误,但修复它并没有解决问题。作为 azure webjob 的应用程序请求未记录在 SendGrid 活动列表中。
  • 您介意重新发布网络作业吗?我在我的网站上进行了测试,一切正常。
  • 现在有更新吗?如果我的回复有帮助,请接受它作为答案(单击答案旁边的复选标记,将其从灰色切换为已填充。)
  • 嗨,乔伊,我看不到您建议的更改。问题还没有解决。
猜你喜欢
  • 1970-01-01
  • 2018-01-11
  • 2016-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多