Application Emails with Amazon Simple Email Service

By Alex Hyett on in Software Developent

Most web applications at some point need a way to send emails. Whether it is just a welcome email, password reset email or emails for exceptions. There are a number of email providers out there you can use to send transactional emails.

When I set up GrowRecruit I originally used SendGrid. SendGrid costs $9.95 a month for up to 40,000 emails. However, they recently sent me an email saying as of 1st January the price is going up to $14.95 per month.

Given I am only using this for sending password reset emails and exceptions I send less than 20 emails a month.

I had been planning on switching over to Amazon Simple Email Service (SES) for a little while now and this was just the push I needed.

What is Amazon’s Simple Email Service?

Amazon SES is a cloud-based email sending service for digital marketers and developers for sending marketing, notification and transactional emails. For low volumes of emails, it is incredibly cheap if not free to use.

What does Amazon SES Cost?

There are no monthly fees for Amazon SES and the cost can even be free if you are hosting your application using Amazon EC2.

For EC2 hosted applications you get 62,000 emails a month free, and the cost is just $0.10 for every 1,000 emails after that. If you send attachments with your email it will cost an additional $0.12 for each GB you send.

For non-EC2 hosted applications it costs just $0.10 for every 1,000 emails you send along with the same charges for attachments.

I am not using this for receiving email but if you do you get the first 1,000 emails a month free.

How to Set Up Amazon SES

You will obviously need an AWS account to get started. I am not going to go into the details here. Once you log in you will need to search for Simple Email Service in the search bar.

Verify your domain

To get started you need to add the domain you plan on sending email from. Click Domains under Identity Management from the menu and click Verify New Domain.

Enter your domain and tick the Generate DKIM Settings checkbox. DKIM signatures are a way for email servers to verify that the email comes from your domain and avoid it being marked as spam.

On the next screen, it will list all the DKIM and TXT verification settings you need to apply. If you are using Route 53 for your domain DNS there is a button at the bottom you can click to set all this up for you. Leave the MX records unchecked unless you plan on receiving mail with SES too.

Add the sender email addresses

Once Amazon has validated your domain you need to validate all the email address you plan on sending email from. You will need to have your MX records set up correctly as Amazon will send you a link you need to click on to validate the address.

If you want to use a different email address in future you will need to go through this process again/

Get out of Sandbox

By default, Amazon puts you in Sandbox mode when you first signup for an account. If you try and send an email while in sandbox you will likely get the following error from your application:

Message rejected: Email address is not verified. The following identities failed the check-in region REGION: RECIPIENT-EMAIL-ADDRESS

This is because while in Sandbox you have to also verify all the recipient email addresses too. So basically you can only send emails to yourself, not overly useful.

If you go to the Sending Statistics page you will likely see something like this: Amazon SES Sandbox Mode

As the message suggests you need to send a request to support to get your sending limit increased. Amazon has a support page which outlines what you need to include in your support request.

It took about 24 hours but Amazon did approve my account and set my sending limit to 50,000 emails a day. It is going to be a long time before I go over that!

Ways to connect

Once you are out of Sandbox there are 2 main ways to use SES, the API and SMTP.

If you are coming from another email provider then you are likely using SMTP to send emails. You need to create some credentials for your application to use SMTP.

To set up credentials go to Email Sending > SMTP Settings and click the Create My SMTP Credentials button.

Once created you will get a username and password you can use to send emails. Plug that into your application and you are good to go.

Sending Emails in a .NET Core Application

I am sure there are a million different ways to send emails in .Net Core. I have found one of the simplest is to use MailKit and MimeKit.

With these imported you can send an email like this:

var message = new MimeMessage();
message.From.Add(new MailboxAddress("The Name of the Sender", _settings.SenderEmailAddress));
message.To.Add(new MailboxAddress(to));
message.Subject = subject;
message.Body = new TextPart(TextFormat.Text)
{
    Text = body
};

using(var client = new SmtpClient())
{
    client.Connect(_settings.SmtpServer, _settings.Port, _settings.Port == 465);
    client.AuthenticationMechanisms.Remove("XOAUTH2");
    await client.AuthenticateAsync(_settings.Username, _settings.Password);
    await client.SendAsync(message);
    await client.DisconnectAsync(true);
}

In the above example, I have the email server settings stored in appsettings.json. These then just get overridden by Docker environment variables in production.

If you need a mail server for development you can either run one locally or use something like MailTrap.



Alex Hyett
WRITTEN BY

Alex Hyett

Software Developer, Founder of GrowRecruit, Entrepreneur, Father, and Husband. @thealexhyett. Currently Technical Lead at Checkout.com.