Sending Email from Azure with SendGrid

In SharePoint Online I need to send email(s) when a user updates a document. Who this email goes to involves some fairly complex logic – which we’ll ignore for this post. So how do we send the email? SharePoint Online doesn’t allow you to use SPUtility.SendEmail() or System.Net.Mail.SmtpClient – so unless you’re using standard alerts, or the workflow “Send Email” activity, well, you’re going to have to look elsewhere. I didn’t think SharePoint designer Workflows were quite up to the complexity of it, so I started to look elsewhere – at Remote Event Receivers, provider-hosted in Azure.

This leads to the question “can Azure send email?” I can imagine a few reasons why it maybe shouldn’t (think “spam”).

Well, Azure can’t send email, apparently – but it has an add-on “SendGrid” that can. I added it to the Azure instance (hint: make sure you have a valid credit card recorded against your subscription – mine wasn’t!) and started to try to follow some of the instructions on the ‘How to Send Email Using SendGrid with Windows Azure‘.

Unfortunately, this got a little confusing – there are different versions of SendGrid available through NuGet:

NuGet

Hmm. SendGridPlus – things with a “Plus” are better, right, like Google+ or AndyPlus? Obviously, I added that to my project. Unfortunately, this means the API examples from SendGrid don’t work – SendGridPlus is quite different in it’s API. Below is how to send an email in either of them:

SendGrid: (From their example, not actually tested)

var from = new MailAddress("donotreply@example.com");
var to   = new MailAddress[] { new MailAddress("andy@example.com") };
var cc   = new MailAddress[] { new MailAddress("bob@example.com") };
var bcc  = new MailAddress[] { new MailAddress("cath@example.com") };
var subject = "Andy Was here!";
var html    = "<b>Andy</b> was here working on item: " + item.Id.ToString();
var text 	= "Andy really was here working on item: " + item.Id.ToString();
var transport = SendGridMail.TransportType.SMTP;

// Create an email, passing in the the eight properties as arguments.
SendGrid myMessage = SendGrid.GetInstance(from, to, cc, bcc, subject, html, text, transport);

var credentials = new NetworkCredential("username", "password");

// Create an SMTP transport for sending email.
var transportSMTP = SMTP.GetInstance(credentials);

// Send the email.
transportSMTP.Deliver(myMessage);

SendGridPlus (Worked for me!):

List<MailAddress> to = new List<MailAddress>();
List<MailAddress> cc = new List<MailAddress>();
List<MailAddress> bcc = new List<MailAddress>();
to.Add(new MailAddress("andy@example.com"));

Mail msg = SendGrid.Mail.GetInstance(new MailAddress("donotreply@example.com"),
 to.ToArray(),
 cc.ToArray(),
 bcc.ToArray(),
 "Andy Was here!",
 "<b>Andy</b> was here working on item: " + item.Id.ToString(),
 "Andy really was here working on item: " + item.Id.ToString());

var credentials = new NetworkCredential("username", "password");

// Create an SMTP transport for sending email.
var transportSMTP = SMTP.GetInstance(credentials);

// Send the email.
transportSMTP.Deliver(msg);

As you can see, both are pretty similar – but GetInstance() is a little different, and I prefer my use of generic lists (it’s a shame SendGrid’s API doesn’t use this (hint!))

Username and Password are your account details from Azure. You can find them through the Azure Management Portal.

I’m really not clear what you hand the GetInstance() function if you won’t want any CC or BCC addresses – zero-length arrays seem to work, but it feels a bit weird. Do not use NULL. This seems to cause some kind of error, but it’s a bit weird – no exception is thrown (that I could see), just the remote event receiver call terminates.

The result – email:

Email

To be honest, the emails arrived pretty quickly – I was fairly impressed. Oh, and what does it cost? For up to 25,000 emails per month – free:

SendGrid cost

So, sending email from Azure with SendGridPlus in an remote event receiver – pretty straight forward.

Advertisements
Sending Email from Azure with SendGrid

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s