【问题标题】:Why are my emails sent from my ASP.Net core web app using Azure SendGrid sometimes not being received为什么有时收不到使用 Azure SendGrid 从我的 ASP.Net 核心 Web 应用程序发送的电子邮件
【发布时间】:2020-09-16 17:51:06
【问题描述】:

我正在使用 Azure Sendgrid 从我的 ASP.net 核心 Web 应用程序发送电子邮件。有时,某些收件人没有收到电子邮件。有些收件人会收到电子邮件,有些则不会。

以下代码为简洁起见,不包括设置 MailMessage(FROM、TO 等...)

我使用“smtp.sendgrid.net”作为 smtpserver,“端口”587 作为 smtpport。

string smtpServer = ConfigurationManager.AppSettings["MailSMTPServer"];
   int smtpPort = int.Parse(ConfigurationManager.AppSettings["MailPort"]);
   string username = ConfigurationManager.AppSettings["MailUserName"];
   string pwd = ConfigurationManager.AppSettings["MailPassword"];


   SmtpClient client = new SmtpClient(smtpServer);

   client.Port = smtpPort;
   client.UseDefaultCredentials = false;
   client.Credentials = new NetworkCredential(username, pwd);
   client.DeliveryMethod = SmtpDeliveryMethod.Network;

   client.Send(mailMessage);

我已与未收到电子邮件的 IT 部门进行了交谈,以查看它们是否被阻止。没有电子邮件到达其服务器的记录。

【问题讨论】:

  • SendGrid 控制台会告诉您关于未收到电子邮件的哪些信息?

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


【解决方案1】:

recommended 通过 SMTP 使用他们的 REST API

  • SMTP 中继会产生多个潜在故障点,这意味着发件人和 SendGrid 可能会导致其中一条“不正常”消息。
  • Web API 更快。 SMTP 中继中所需的额外来回可能会导致外部客户的一些(最小)延迟 位于美国,因此离我们的入站数据中心更远。
  • Web API 还允许您通过使用API keys 为您的程序添加额外的安全层。 API 密钥允许您生成一个 身份验证凭证与您的用户名密码分开,其中 显着降低有人使用您的帐户发送的机会 垃圾邮件或网络钓鱼。

另外,outbound SMTP restriction in Azure 可能存在白名单问题。如果您改用 REST API,则可以克服这一点。

这是一个C# example,使用他们的nuget

var apiKey = "<send grid api key>";
var client = new SendGridClient(apiKey);
var from = new EmailAddress("test@example.com", "Example User");
var subject = "Sending with SendGrid is Fun";
var to = new EmailAddress("test@example.com", "Example User");
var plainTextContent = "and easy to do anywhere, even with C#";
var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2018-01-08
    • 2015-02-08
    相关资源
    最近更新 更多