Unable to send emails on Azure Web App using Sendgrid

情到浓时终转凉″ 提交于 2019-12-12 03:14:30

问题


I am unable to send emails on a MVC4 Web App that I configured on Azure. I am being able to send emails locally on my machine, but when I upload the code to my azure app in the cloud, the mail delivery doesn't say it fails, but emails are not delivered, SendGrid doesn't report that the mail was sent on its dashboard either.

Has someone ran into similar issues? Have looked into similar questions with no exact answer.

I did follow the instructions of this article (https://azure.microsoft.com/en-us/documentation/articles/sendgrid-dotnet-how-to-send-email/#reference) without success. Have tried also to the send the emails thru the SendGrid class libraries and by using plain System.Net.Mail.

Any help is appreciated.

Code:

Thanks for the reply. I am using Web.DeliverAsync, this is the code I am using:

// Create network credentials to access your SendGrid account
var username = System.Configuration.ConfigurationManager.AppSettings["smtpUser"];
var pswd = System.Configuration.ConfigurationManager.AppSettings["smtpPass"];

var credentials = new NetworkCredential(username, pswd);

// Create an Web transport for sending email.
SendGrid.Web transportWeb = new SendGrid.Web(credentials);

//var apiKey = System.Configuration.ConfigurationManager.AppSettings["sendgrid_api_key"];
var apiKey = "apikey";
SendGridMessage msg = new SendGridMessage();

msg.Subject = "...";
msg.AddTo(model.Email);
msg.Html = body;
msg.From = new MailAddress("...");

try
{
await transportWeb.DeliverAsync(msg);
}
catch (Exception e)
{
  e.ToExceptionless().Submit();
  ViewBag.ErrorMsg = e.Message;
  return View("Error");
}

回答1:


Try this:

var apiKey = System.Configuration.ConfigurationManager.AppSettings["sendgrid_api_key"];

// Create an Web transport for sending email.
SendGrid.Web transportWeb = new SendGrid.Web(apiKey);

SendGridMessage msg = new SendGridMessage();

msg.Subject = "...";
msg.AddTo(model.Email);
msg.Html = body;
msg.From = new MailAddress("...");

try
{
  await transportWeb.DeliverAsync(msg);
}
catch (Exception e)
{
  //e.ToExceptionless().Submit();
  ViewBag.ErrorMsg = e.Message;
  return View("Error");
}

Also what version of the SendGrid C# library are you using? Make sure it's version 6.3.x or later.



来源:https://stackoverflow.com/questions/34860369/unable-to-send-emails-on-azure-web-app-using-sendgrid

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