繁体   English   中英

为什么有时收不到使用 Azure SendGrid 从我的 ASP.Net 核心 Web 应用程序发送的电子邮件

[英]Why are my emails sent from my ASP.Net core web app using Azure SendGrid sometimes not being received

我正在使用 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 部门进行了交谈,以查看它们是否被阻止。 没有电子邮件到达他们服务器的记录。

建议通过 SMTP 使用他们的REST API

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

此外,由于Azure 中的出站 SMTP 限制,可能存在白名单问题。 如果您改用 REST API,则可以克服这个问题。

这是使用他们的nugetC# 示例

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM