AWS

AWS Simple Email Service (SES)

What are we talking about this time?

Last time we talked about Step Functions.This time we will be talking about Simple Email Service (SES). I don’t want to cover everything about this service, but I do want to show you how you can use it so send emails as a bare minimum.

Initial setup

If you did not read the very first part of this series of posts, I urge you to go and read that one now as it shows you how to get started with AWS, and create an IAM user : https://sachabarbs.wordpress.com/2018/08/30/aws-initial-setup/

Where is the code

The code for this post can be found here in GitHub : https://github.com/sachabarber/AWS/tree/master/AppServices/SES

What are we talking about this time?

Ok so as I stated above this time we are going to be talking about SES, which is going to be fairly self contained, and a small post this time to be honest. I just want to show how we can use SES to send mails from our own apps.

Setting It Up And Sending An Email

Before we start it is important to note that the SES service is limited to a few regions. So my normal EU-WEST2 is NOT supported. So I have to use EU-WEST1. 

So the first step is to setup a verified email which you can do in the SES console here

image

Once you have done that you can use the verification email that gets sent to the email address you used to complete the verification process. Then you should be good to use this email as a sender for SES. You can read more about this process here : https://docs.aws.amazon.com/ses/latest/DeveloperGuide/setting-up-email.html

But assuming you have done this step, it really is as simple as making sure you are using the correct region, and then using the verified email you just created and using some code like this

using Amazon;
using System;
using System.Collections.Generic;
using System.Configuration;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;
using Amazon.Runtime;

namespace SESSender
{
    class Program
    {
        // Set the sender's email address here : AWSVerifiedEmail
        static readonly string senderAddress = ";

        // Set the receiver's email address here.
        static readonly string receiverAddress = "";

        static void Main(string[] args)
        {
            if (CheckRequiredFields())
            {

                using (var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.EUWest1))
                {
                    var sendRequest = new SendEmailRequest
                    {
                        Source = senderAddress,
                        Destination = new Destination { ToAddresses = new List<string> { receiverAddress } },
                        Message = new Message
                        {
                            Subject = new Content("Sample Mail using SES"),
                            Body = new Body { Text = new Content("Sample message content.") }
                        }
                    };
                    try
                    {
                        Console.WriteLine("Sending email using AWS SES...");
                        var response = client.SendEmail(sendRequest);
                        Console.WriteLine("The email was sent successfully.");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("The email was not sent.");
                        Console.WriteLine("Error message: " + ex.Message);

                    }
                }
            }

            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }

        static bool CheckRequiredFields()
        {
            var appConfig = ConfigurationManager.AppSettings;

            if (string.IsNullOrEmpty(appConfig["AWSProfileName"]))
            {
                Console.WriteLine("AWSProfileName was not set in the App.config file.");
                return false;
            }
            if (string.IsNullOrEmpty(senderAddress))
            {
                Console.WriteLine("The variable senderAddress is not set.");
                return false;
            }
            if (string.IsNullOrEmpty(receiverAddress))
            {
                Console.WriteLine("The variable receiverAddress is not set.");
                return false;
            }
            return true;
        }
    }
}

Remember this service IS NOT available in all regions, so make sure you have the correct region set.

Anyway here is the result of sending the email

image

 

SMTP

To use SMTP instead you need to setup the SMTP user in SES console

image

Once you have that you can then just use code something more like this

 

using System;
using System.Net;
using System.Net.Mail;
using System.Configuration;


namespace SESSMTPSample1
{
    class Program
    {
        // Set the sender's email address here.
        static string senderAddress = null;

        // Set the receiver's email address here.
        static string receiverAddress = null;
        
        // Set the SMTP user name in App.config 
        static string smtpUserName = null;

        // Set the SMTP password in App.config 
        static string smtpPassword = null;

        static string host = "email-smtp.eu-west-1.amazonaws.com";

        static int port = 587;

        static void Main(string[] args)
        {
            if (CheckRequiredFields())
            {
                var smtpClient = new SmtpClient(host, port);
                smtpClient.EnableSsl = true;
                smtpClient.Credentials = new NetworkCredential(smtpUserName, smtpPassword);

                var message = new MailMessage(
                                from: senderAddress,
                                to: receiverAddress,
                                subject: "Sample email using SMTP Interface",
                                body: "Sample email.");

                try
                {
                    Console.WriteLine("Sending email using SMTP interface...");
                    smtpClient.Send(message);
                    Console.WriteLine("The email was sent successfully.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("The email was not sent.");
                    Console.WriteLine("Error message: " + ex.Message);
                }
            }

            Console.Write("Press any key to continue...");
            Console.ReadKey(); 
        }

        static bool CheckRequiredFields()
        {
            var appConfig = ConfigurationManager.AppSettings;

            smtpUserName = appConfig["AwsSesSmtpUserName"];
            if (string.IsNullOrEmpty(smtpUserName))
            {
                Console.WriteLine("AwsSesSmtpUserName is not set in the App.config file.");
                return false;
            }

            smtpPassword = appConfig["AwsSesSmtpPassword"];
            if (string.IsNullOrEmpty(smtpPassword))
            {
                Console.WriteLine("AwsSesSmtpPassword is not set in the App.config file.");
                return false;
            }
            if (string.IsNullOrEmpty(senderAddress))
            {
                Console.WriteLine("The variable senderAddress is not set.");
                return false;
            }
            if (string.IsNullOrEmpty(receiverAddress))
            {
                Console.WriteLine("The variable receiverAddress is not set.");
                return false;
            }
 
            return true;
        }
    }
}

 

And that is all I really wanted to show in this short post. Next time we will likely dip back into Compute stuff, where we look at AWS SWF

Leave a comment