0

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");
}
  • please post actual code that pertains to your current issue and or problem. posting a link really does nobody any good especially if you are not doing exactly in your code vs what the code is doing in the posted link. it's easier to post the actual code you are using so others can try to pinpoint what the issue(s) are that you are experiencing – MethodMan Jan 18 '16 at 17:27
  • Thanks for the replies, attached the code sample. – Alonso Blanco Jan 18 '16 at 17:33
  • 1
    Any update on this matter? I'm having the similar kind of an issue. My code works in localhost and Azure Dev site, but production doesn't send any mail at all. – Geethanga May 16 '16 at 15:27

1 Answers1

0

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.

Justin Steele
  • 2,230
  • 13
  • 16