无法通过SendGrid发送电子邮件

时间:2014-06-08 21:25:54

标签: c# azure sendgrid

我关注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);

6 个答案:

答案 0 :(得分:3)

这是否恰好是控制台应用程序?我一直在测试SendGrid,发现当我尝试从控制台应用程序发送电子邮件时,电子邮件从未发送过。但是,当我从Web应用程序(使用相同的发送代码)尝试时,会发送电子邮件。我没有解释为什么控制台应用程序不起作用。

答案 1 :(得分:2)

我能够使用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());
}

答案 2 :(得分:2)

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;

答案 3 :(得分:2)

我知道这是一篇旧帖子,但我发现为什么电子邮件不是从控制台应用程序发送的。当我添加Console.Read();在program.cs类的末尾,它发送电子邮件,但是如果我删除了Console.Read();从代码中,电子邮件不发送。所以我的假设是,如果我让程序在关闭之前稍等一下,那么电子邮件应该发送。

答案 4 :(得分:1)

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

答案 5 :(得分:0)

好的,我想我已经回答了。上面的代码很好。在SendGrid上,您必须批准发送电子邮件地址。登录到您的SendGrid门户并关注以下内容:

  1. 设置->发件人身份验证
  2. 在“单一发件人验证”下,添加所需的电子邮件地址
  3. 点击收件箱中的授权链接和鲍勃的侄子。

希望以上帮助

相关问题