Continuing my way through some of the AWS services I’ve been consuming lately, so let’s pick up with SES (Simple Email Service). Thousands of providers to do this just like any other quality service but again, cost, ease and already being in embedded with other PaaS components made this a strong contender when I was evaluating. Similar to what we did with the SNS class in the previous post, I’ve taken a similar approach when wrapping SES.
Like you are probably getting used to seeing from my approaches, we are going to build a simple interface for what our class will implement
public interface IEmailSender
Task SendEmail(string subject, IList<string> addresses, string body, bool isHtmlContent);
Task SendEmail(string subject, string address, string body, bool isHtmlContent);
What’s going on here. Not much but the methods I want to provide to our clients is
- Pass in a subject for the email
- Provide a list of addresses or a single address
- Is the body of the message HTML or not
I’m sure there other things a library such as this might want to do so of course you extend this even further.
public class SesEmailSender : IEmailSender
private readonly IConfiguration _configuration;
public SesEmailSender(IConfiguration configuration)
_configuration = configuration;
public async Task SendEmail(string subject, IList<string> addresses, string body, bool isHtmlContent)
var accessKey = _configuration.GetValue<string>("ACCESS_KEY");
var secretKey = _configuration.GetValue<string>("SECRET_KEY");
var client = new AmazonSimpleEmailServiceClient(accessKey, secretKey, RegionEndpoint.USWest2);
var sendRequest = new SendEmailRequest
Source = _configuration.GetValue<string>("SENDER_EMAIL"),
Destination = new Destination
ToAddresses = addresses.ToList()
sendRequest.Message = new Message
Subject = new Content(subject),
Body = new Body
Html = new Content
Charset = "UTF-8",
Data = body
public async Task SendEmail(string subject, string address, string body, bool isHtmlContent)
Just like in the SNS class, the constructor takes on an IConfiguration which is injected in. You will need an IAM user that has access to send emails. This is again outside of the scope of this post, but feel free to message me in the comments and I’ll be happy to help you debug if I can.
The SendEmail method that takes an address as you can see just calls the overloaded version which takes an array. So let’s briefly walk through what that method is doing.
AmazonSimpleEmailServiceClient is the SDK class that does the heavy lifting. What our method does is build up a
SendEmailRequest that can be passed into the client where the magic happens. I’m not currently doing anything with the isHtml flag but of course you could format the message accordingly if you want to handle plain text and/or HTML.
And that’s it. Super simple. I’m using a more enhanced version of this @ 30M + times a day and dotnet core and AWS’ Lambda have no trouble handling that kind of volume.
I’ll probably take a detour for my next couple of posts and then come back to Kinesis and SQS with a Lambda and C#
Thanks for reading!