gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:23:18 25 4
gpt4 key购买 nike

我正在使用 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 andSendGrid 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 outsideof 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 anauthentication credential separate from your username password, whichsignificantly lowers the chance of someone using your account to sendspam 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com