SendGrid SMTP集成问题

AaB*_*aBa 3 c# smtp azure sendgrid

我正在尝试在Azure上使用SmtpClient和MailMessage方法将SendGrid集成到ASP.NET MVC应用程序中。

码:

MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.Subject = "Subject";
message.To.Add("To@MyDomain.com");
message.Body = "Body";
message.From = new MailAddress("From@MyDomain.com", "From Name");

SmtpClient client = new SmtpClient("smtp.sendgrid.net");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("??", "SG.******");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Port = 587; // I have tried with 25 and 2525
client.Timeout = 99999;
client.EnableSsl = false;

client.Send(message);
Run Code Online (Sandbox Code Playgroud)

我最终在Azure VM上的C#控制台应用程序和ASP.NET MVC应用程序中都遇到了此问题:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
Run Code Online (Sandbox Code Playgroud)

根据SendGrid网站上的文档avlb:https://sendgrid.com/docs/Classroom/Basics/Email_Infrastructure/recommended_smtp_settings.html

用户名和密码必须:使用字符串“ apikey”作为SMTP用户名,并使用您的API密钥作为密码。

我试过了 -

  1. 我用来登录其门户的SendGrid用户名

  2. 此页面上的API密钥https://app.sendgrid.com/settings/api_keys

  3. 以SG *开头的密码,该密码取决于其支持团队。

有什么建议我会丢失,或者我应该使用哪个UserName / APIKey?

Tx!

AaB*_*aBa 6

链接说:使用字符串“ apikey”作为SMTP用户名,并使用API​​密钥作为密码。

https://sendgrid.com/docs/Classroom/Basics/Email_Infrastructure/recommended_smtp_settings.html

用户名应该是“ apikey”,而不是实际的“ api key或api name”

client.Credentials = new NetworkCredential("apikey", 
                                           "<Your API Key which statats with SG.*>");
Run Code Online (Sandbox Code Playgroud)

添加“ apikey”(面部手掌)解决了我的问题。