.Net Core guide to consume AWS SES email API

By | June 9, 2019

Amazon SES is one of AWS cloud offerings to send emails. As lot of enterprises moving toward cloud, one of preferred cloud provider is AWS cloud. Typically, these days emails occupied an central place in our day-to-day communications. When you use Amazon SES, Amazon SES becomes your outbound email server. Amazon SES has lot of features and is fully configurable. One feature which stands out is the ability to stream email metrics to Amazon CloudWatch for later analysis and ability to persist incoming emails to Amazon S3 bucket automatically.

There are several ways that you can send an email using Amazon SES. You can use the Amazon SES console, the Simple Mail Transfer Protocol (SMTP) interface, or you can call the Amazon SES API.

There are several ways that you can send an email using Amazon SES. You can use the Amazon SES console, the Simple Mail Transfer Protocol (SMTP) interface, or you can call the Amazon SES API. For the purpose of this article, we’ll be exploring Amazon SES API approach using AWS .NET SDK

AWS SES API with .NET SDK

AWS offers .NET SDK making it easier for .NET applications to integrate AWS SES service. The SDK includes base libraries and documentation. However, lot of this information is scattered around and there is no single place where we can quickly find resources and build S3 solutions for .NET platform. Most of AWS documentation is sparse. You might find following links useful to understand Amazon SES service further more

Like any other Amazon Service, to use Amazon SES email service, you’ll need valid AWS credentials. Check this link for getting started with setting up Amazon SES account
https://aws.amazon.com/ses/getting-started/

To use Amazon SES API in .NET applications, you’ll need at minimum an access key and secret key. Please note, how to best manage AWS credentials/keys and IAM roles/users is all together a different topic. For sake of this article, it is assumed that you have access to a shared credentials file. For example, you can locate AWS credential file in windows at %USERPROFILE%\.aws\credentials

Adding AWS Credentials to .NET Core App

Considering, you have successfully obtained AWS credential, next step is to add those credentials into .NET Core application.

AWS SDK specifically provides special extensions AWSSDK.Extensions.NETCore.Setup to make AWS SDK compatible with new .NET Core IConfiguration system. You’ll register Amazon SES API IAmazonSimpleEmailService as .NET Core service in your Startup class as shown below

SES API service registration in Startup class
AWS configuration in appSettings.json

Sending emails using Amazon SES

Here’s C# classAwsEmailService class built on top of Amazon SES API IAmazonSimpleEmailService. This helper class leverages flexible SendRawEmail API method. When you use the SendRawEmail operation, you can specify the headers of the message as well as its content. This flexibility is useful, for example, when you want to send a multipart MIME email. You can also use this operation to send messages that include attachments.So, this helper class is capable of sending file attachments in the outgoing emails. You can either provide FileStream, FilePath as attachments

However, please note SendRawEmail operation has the following requirements:

  • You can only send email from verified email addresses.
  • The maximum message size, including attachments, is 10 MB.

IAwsEmailService.cs

AwsEmailService.cs

Email test

we created a working test controller which will act as email client sending emails programmatically using Amazon SES API

You can download full source code from this below link