What I Learned Building a Startup on Microsoft Cloud Services: Part 9 – Sending Email Notifications from Windows Azure

March 14th, 2012

I am the founder of a startup called Cotega and also a Microsoft employee within the SQL Azure group where I work as a Program Manager. This is a series of posts where I talk about my experience building a startup outside of Microsoft. I do my best to take my Microsoft hat off and tell both the good parts and the bad parts I experienced using Azure.

A key feature of the Cotega monitoring service is the ability to send email notifications when there are issues with a user’s database. The Windows Azure worker role executes a specified set of jobs against a users database and if there are issues such as connection drops or query performance degradation, then the service will send a notification to the administrator.

Currently there are no SMTP servers available within Windows Azure to allow me to send email notifications. Luckily, there are a huge number of third party email services that work really well with Windows Azure and are extremely cheap.

Sending Email Using Free Email Services

To get started, I first built a prototype that would send notifications from my worker roles. To do this I started with Hotmail as the SMTP server. The code to do this is pretty simple and there are a number of good examples on how to do this, such as here and here. These services worked pretty well.  Ultimately I decided not to move forward with them given the sending restrictions that free email services like Hotmail, Office365, GMail, Yahoo mail have. For example, many of them limit the number of senders you can send to within an hour or limit the total number of emails you could send over a specified period of time. I suspected it would be a long time before the Cotega service reached these limits but I preferred to avoid them if possible.

3rd Party Paid Email Services

I really did not want to host my own SMTP service in Windows Azure.  In fact, I have heard (but not confirmed) that sending emails using your own SMTP servers can have issues where emails will frequently be bounced back or will be tagged inaccurately as spam.  Many third party email services have techniques to minimize this which was very attractive to me.  So for these reasons, I started researching other third party email services. Some of the most promising ones that I found were Elastic EmailSendGrid and Amazon SES (Simple Email Service). Each of these are paid services.  I ended up using Amazon SES primarily because I wanted to get an opportunity to learn more about Amazon’s services and their cost was attractive at $0.10 per thousand email messages. The other interesting thing about Amazon SES is that they start you out at a limited number of outbound emails. This limit is quite high at 10,000 over 24 hours and increases as Amazon learns to trust that you are not sending spam. Plus you can request an increase if needed. The dashboard for monitoring your email traffic is pretty nice and allows you to visually see the number of delivered, rejected, bounced and complaints emails.

After setting up my Amazon SES account, I needed to install the Amazon SDK and include the Amazon.SimpleEmail.Model namespace which would be deployed a a reference to the worker role. Here is a snippet of code that I used which is based on a sample Amazon provides. If you use it, remember to update the [CODE] sections with your SES keys:

public static Boolean SendEmailSES(String From, String To, String Subject, String Text = null, String HTML = null, String emailReplyTo = null, String returnPath = null)
{
    if (Text != null && HTML != null)
    {
        String from = From;

        List to
            = To
            .Replace(", ", ",")
            .Split(',')
            .ToList();

        Destination destination = new Destination();
        destination.WithToAddresses(to);
        //destination.WithCcAddresses(cc);
        //destination.WithBccAddresses(bcc);

        Content subject = new Content();
        subject.WithCharset("UTF-8");
        subject.WithData(Subject);

        Content html = new Content();
        html.WithCharset("UTF-8");
        html.WithData(HTML);

        Content text = new Content();
        text.WithCharset("UTF-8");
        text.WithData(Text);

        Body body = new Body();
        body.WithHtml(html);
        body.WithText(text);

        Message message = new Message();
        message.WithBody(body);
        message.WithSubject(subject);

        AmazonSimpleEmailService ses = AWSClientFactory.CreateAmazonSimpleEmailServiceClient("[CODE]", "[CODE]");

        SendEmailRequest request = new SendEmailRequest();
        request.WithDestination(destination);
        request.WithMessage(message);
        request.WithSource(from);

        if (emailReplyTo != null)
        {
            List replyto
                = emailReplyTo
                .Replace(", ", ",")
                .Split(',')
                .ToList();

            request.WithReplyToAddresses(replyto);
        }

        if (returnPath != null)
        {
            request.WithReturnPath(returnPath);
        }

        try
        {
            SendEmailResponse response = ses.SendEmail(request);
            SendEmailResult result = response.SendEmailResult;

            Console.WriteLine("Email sent.");
            Console.WriteLine(String.Format("Message ID: {0}",
                result.MessageId));

            return true;
        }
        catch (Exception ex)
        {
            Helper.LogException("Worker - SendEmailSES", ex.Message.ToString());
            return false;
        }
    }

    Console.WriteLine("Specify Text and/or HTML for the email body!");

    return false;
}

3 Responses to “What I Learned Building a Startup on Microsoft Cloud Services: Part 9 – Sending Email Notifications from Windows Azure”

  1. [...] Cavanagh (@liamca) continued his series with What I Learned Building a Startup on Microsoft Cloud Services: Part 9 – Sending Email Notification… on [...]

  2. [...] I had a little background experience on how to send notification. For that service, I used Amazon’s SES to send email notifications. For this Baby Monitor, I chose to use a different service called [...]

RSS feed for comments on this post.

Leave a Reply