1
votes

I'm trying to use SendGrid to send an email from an Azure worker role every time there are certain exceptions, but I can't get the email to send. I am using SendGridMail version 6.1.0.0 and SendGrid.SmtpApi version 1.3.1.0 which I installed via nuget and .Net 4.5. I am currently debugging locally with plans to deploy to Azure if i can get the emails to successfully send.

SendGridMessage myMessage = new SendGridMessage();
List<String> recipients = new List<String> { @"John Doe <[email protected]>", @"Peter Howe <[email protected]>" };
myMessage.AddTo(recipients);
myMessage.From = new MailAddress("[email protected]");
myMessage.Subject = "Error in Update";
myMessage.Text = "TESTING 123";

string username = XXXXXX;

string password = XXXXXXX;
// Create credentials, specifying your user name and password.
var credentials = new NetworkCredential(username, password);

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

// Send the email.
await transportWeb.DeliverAsync(myMessage);

As far as I can see I'm not getting any errors except when I debug and look at myMessage the Header has an error.

When I tried initializing a new empty header (var header = new Header();) I noticed there were still errors on that

To = 'header.To' threw an exception of type 'System.ArgumentException' Message = "Bad key path!"

Does anyone know what this means? Or if this could be causing the emails not to send?

2

2 Answers

2
votes

The answer to your other question actually uses SendGrid:

Alerts for exceptions in an Azure worker role

There are three globalvariables:

public const string SmtpServerHost = "smtp.sendgrid.net";
public const string SmtpServerUserName = "[[email protected]]";
public const string SmtpServerPassword = "[password from sendgrid]";

You actually do not need to use the SDK, just setup the account in Azure portal, and save your creds in your project.

You can send emails locally, but if you are on a work network, the firewall may block the emails from being sent. The code I posted I placed in an email service in my namespace.

0
votes

It has be deployed to Azure to work. It won't work locally.