Can't send email through SendGrid

天大地大妈咪最大 提交于 2019-12-01 09:19:27

问题


I'm following the example from SendGrid's site and as credentials I'm pasting it what they gave me in the Azure portal. Still, I get this error message.

Message = {"Failure sending mail."}
InnerException = {"Unable to connect to the remote server"}

I'm not clear on what to do here, not even how to debug it. I'm running the code from the desktop (before I put it on the site) so I can break-point myself through it. However, to no joy...

Suggestions?

Full code below.

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);

回答1:


Does this happen to be a console application? I've been testing SendGrid and found that when I try to send an email from a Console application the emails are never sent. However, when I tried it from a web application (using the same exact sending code) the emails are sent. I have no explanation for why the console application does not work.




回答2:


I was able to get a SendGrid email send to happen successfully from a Console application using the SendGrid v3 C# API.

I first needed to import the SendGrid C# library via NuGet using the Package Manager Console in Visual Studio:

PM> Install-Package SendGrid

Then, my code:

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());
}



回答3:


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;
}

Include using SendGrid; in your project.




回答4:


I know this is an old post, but I found out why emails are not sent from a console application. When I add Console.Read(); at the end of the program.cs class, it sends the emails, but if I remove the Console.Read(); from the code, the emails do not send. So my assumption is, if I let the program wait a bit before it closes, the emails should send.



来源:https://stackoverflow.com/questions/24111000/cant-send-email-through-sendgrid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!