azure 无法通过SendGrid发送电子邮件

ybzsozfc  于 2022-11-30  发布在  其他
关注(0)|答案(6)|浏览(255)

我按照SendGrid's site中的例子,将他们在Azure门户中提供的内容作为凭据粘贴。但是,我仍然收到此错误消息。
消息= {“发送邮件失败。"}
InnerException = {“无法连接到远程服务器”}
我不清楚在这里要做什么,甚至不知道如何调试它。我正在从桌面运行代码(在我把它放在网站上之前),这样我就可以突破自己通过它。然而,没有喜悦...
有什么建议?
完整代码如下。

MailMessage mailMsg = new MailMessage();
mailMsg.To.Add(new MailAddress("to@example.com", "To Name"));
mailMsg.From = new MailAddress("from@example.com", "From Name");
mailMsg.Subject = "subject";
string text = "text body";
string html = @"<p>html body</p>";
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(
  text, null, MediaTypeNames.Text.Plain));
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(
  html, null, MediaTypeNames.Text.Html));
SmtpClient client = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(
  "azure_...@azure.com",
  "LubX........pQc");
client.Credentials = credentials;
client.Send(mailMsg);
amrnrhlw

amrnrhlw1#

这是一个控制台应用程序吗?我一直在测试SendGrid,发现当我尝试从控制台应用程序发送电子邮件时,电子邮件从来没有发送过。但是,当我尝试从一个Web应用程序(使用相同的发送代码)发送电子邮件时,电子邮件却发送了。我无法解释为什么控制台应用程序不工作。

nhn9ugyo

nhn9ugyo2#

我能够使用SendGrid v3 C# API从控制台应用程序成功发送SendGrid电子邮件。
我首先需要使用Visual Studio中的包管理器控制台通过NuGet导入SendGrid C#库:

PM> Install-Package SendGrid

然后,我的代码:

using SendGrid;
using SendGrid.Helpers.Mail;
using System.Threading.Tasks;

// ...

public static async Task SendEmailViaSendGrid(string to, string from, string subject, string body)
{
    // Generated from https://app.sendgrid.com/settings/api_keys
    // TODO: Store this somewhere secure -- not in version control.
    string apiKey = "YOUR_PRIVATE_SENDGRID_API_KEY_GOES_HERE";

    dynamic sendGridClient = new SendGridAPIClient(apiKey);

    Email fromEmail = new Email(from);
    Email toEmail = new Email(to);
    Content content = new Content("text/plain", body);
    Mail mail = new Mail(fromEmail, subject, toEmail, content);

    dynamic response = await sendGridClient.client.mail.send.post(requestBody: mail.Get());
}
x6492ojm

x6492ojm3#

public static bool SendMailBySendGrid(string offMail, string mailFormatDetails,string subject,string ccMail=null,string bccMail=null)
{
  string mailFromId = System.Configuration.ConfigurationManager.AppSettings["Sender"];
  SendGridMessage sndmsg = new SendGridMessage();
  sndmsg.From = new MailAddress(mailFromId);
  sndmsg.To = new MailAddress[] { new MailAddress(offMail) };
  if (!string.IsNullOrEmpty(ccMail))
    sndmsg.Cc = new MailAddress[] { new MailAddress(ccMail) };
  if(!string.IsNullOrEmpty(bccMail))
    sndmsg.Bcc = new MailAddress[] { new MailAddress(bccMail) };
  sndmsg.Subject = subject;         
  sndmsg.Html = "<div>" + mailFormatDetails + "</div>";
  bool result= SendEmail(sndmsg);
  return result;
}

包括 * 使用SendGrid; * 在项目中。

lawou6xi

lawou6xi4#

我知道这是一个老帖子,但我发现了为什么电子邮件不是从控制台应用程序发送的。当我添加Console.read();在program.cs类的末尾,它发送电子邮件,但是如果我删除Console.Read();从代码来看,电子邮件不会发送。所以我的假设是,如果我让程序在关闭前等待一段时间,电子邮件应该会发送。

2nbm6dog

2nbm6dog5#

不确定是否每个人都是这种情况,但我必须创建一个Sender. https://app.sendgrid.com/settings/sender_auth/senders。这将匹配mailMsg.From

rjee0c15

rjee0c156#

好的,我想我有答案了。上面的代码没有问题。在SendGrid上,您必须批准发送电子邮件地址。登录您的SendGrid门户并注意以下事项:
1.设置-〉发件人认证
1.在“Single Sender Verification”(单个发件人验证)下,添加所需的电子邮件地址
1.点击收件箱中的授权链接和你的鲍勃的侄子。
希望以上内容对您有所帮助

相关问题