Application Emails with Amazon Simple Email Service

Application Emails with Amazon Simple Email Service

by | 4 min read
Published:
Updated:

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.

Amazon Simple Email Service

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.

Verify 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

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.

Amazon SES SMTP

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.


🙏 Was this helpful? If you want to say thanks, I love coffee ☕️ , any support is appreciated.


ALSO ON ALEXHYETT.COM

What is Event Driven Architecture?

What is Event Driven Architecture?

  • 14 April 2023
One of the leading architecture patterns used with microservices is event-driven architecture. Event-driven architecture has many benefits…
Hosting n8n for Free with Railway

Hosting n8n for Free with Railway

  • 30 January 2023
I have been using n8n for a couple of months now, and it has allowed me to automate so much of my daily workflow. These are some of the…
Using GitHub Actions to Deploy to S3

Using GitHub Actions to Deploy to S3

  • 26 March 2021
Recently I went through the process of setting up Drone CI on my Raspberry Pi. The plan was to use my Raspberry Pi as a build server for…
Getting Started with AWS Step Functions

Getting Started with AWS Step Functions

  • 12 March 2021
I have recently been looking into AWS Step Functions. For those not familiar with them, Step Functions are Amazon’s way of providing a state…
Useful Docker Commands Worth Saving

Useful Docker Commands Worth Saving

  • 12 February 2021
I use docker every day. All the applications I write at work or at home end up in docker containers. Most of the time though, I am only…
Grafana Monitoring on a Raspberry Pi

Grafana Monitoring on a Raspberry Pi

  • 28 January 2021
As you might have seen from my last few posts I have quite a lot running on my Raspberry Pi. I am currently using a Raspberry Pi 2 B which…
How to set up Drone CI on Raspberry Pi (and why you shouldn't)

How to set up Drone CI on Raspberry Pi (and why you shouldn't)

  • 27 January 2021
I wanted to put together my home build server using my Raspberry Pi. After looking at the options I picked Drone CI, it has a nice interface…
Traefik vs Nginx for Reverse Proxy with Docker on a Raspberry Pi

Traefik vs Nginx for Reverse Proxy with Docker on a Raspberry Pi

  • 20 January 2021
I use my Raspberry Pi as my own personal home server. Up until recently, I have been using nginx as a reverse proxy for my docker containers…