c# - 为什么有时无法收到使用 Azure SendGrid 从 ASP.Net core Web 应用程序发送的电子邮件

标签 c# azure asp.net-core sendgrid

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

最佳答案

这是 recommended使用他们的 REST API通过 SMTP。

  • SMTP relay creates multiple points of potential failure, meaning that there are more places in the conversation between the sender and SendGrid that could cause one of those “not okay” messages.
  • The Web API is faster. The extra back and forth necessary in SMTP relay can cause some (minimal) latency for customers who are outside of the United States, and thus further from our inbound data centers.
  • The Web API also allows you to add an extra layer of security to your program by utilizing API keys. API keys allow you to generate an authentication credential separate from your username password, which significantly lowers the chance of someone using your account to send spam or phish.

此外,由于 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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="671302141327021f060a170b024904080a" rel="noreferrer noopener nofollow">[email protected]</a>", "Example User");
var subject = "Sending with SendGrid is Fun";
var to = new EmailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e5a4b5d5a6e4b564f435e424b004d4143" rel="noreferrer noopener nofollow">[email protected]</a>", "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);

关于c# - 为什么有时无法收到使用 Azure SendGrid 从 ASP.Net core Web 应用程序发送的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63925458/

相关文章:

C# MVC 核心自定义 Bootstrap CSS

c# - 使用Unity JsonUtility反序列化JSON数组获取序列化值失败

azure - 基于 token 的数据库身份验证失败,错误代码为 "Login failed for user ' NT AUTHORITY\ANONYMOUS LOGON'。”

c# - ASP.Net 5 (vnext) 如何排队后台任务和访问 ApplicationServices

azure - 是否可以在云上部署分布式系统?

Azure管道##vso[build.addbuildtag]不起作用

c# - 为什么我应该始终为我的 Authorize 属性定义 JwtBearerDefaults.AuthenticationScheme?

c# - 如何让Tesseract OCR以句子形式输出单词?

c# - System.NotSupportedException 目前不支持方法 'System.Linq.Queryable.GroupBy' 的这种重载。

c# - 如何将 AutomationElement.NativeWindowHandle 转换为 IntPtr