Menu
Amazon Simple Email Service
Developer Guide

Send an Email Using the AWS SDK for .NET

The following procedure shows you how to send an email through Amazon SES using Visual Studio and the AWS SDK for .NET.

This solution was tested using the following components:

  • Microsoft Visual Studio Community 2017, version 15.4.0.

  • Microsoft .NET Framework version 4.6.1.

  • The AWSSDK.Core package (version 3.3.19), installed using NuGet.

  • The AWSSDK.SimpleEmail package (version 3.3.6.1), installed using NuGet.

Note

In this getting started tutorial, you send an email to yourself so that you can check to see if you received it. For further experimentation or load testing, use the Amazon SES mailbox simulator. Emails that you send to the mailbox simulator do not count toward your sending quota or your bounce and complaint rates. For more information, see Testing Email Sending in Amazon SES.

Prerequisites

Before you begin, perform the following tasks:

  • Verify your email address with Amazon SES—Before you can send an email with Amazon SES, you must verify that you own the sender's email address. If your account is still in the Amazon SES sandbox, you must also verify the recipient email address. The easiest way to verify email addresses is by using the Amazon SES console. For more information, see Verifying Email Addresses in Amazon SES.

  • Get your AWS credentials—You need an AWS access key ID and AWS secret access key to access Amazon SES using an SDK. You can find your credentials by using the Security Credentials page of the AWS Management Console. For more information about credentials, see Using Credentials With Amazon SES.

  • Install Visual Studio—Visual Studio is available at https://www.visualstudio.com/.

  • Create a shared credentials file—For the sample code in this section to function properly, you must create a shared credentials file. For more information, see Create a Shared Credentials File.

Procedure

The following procedure shows how to send an email through Amazon SES using the AWS SDK for .NET.

To send an email using the AWS SDK for .NET

  1. Create a new project by performing the following steps:

    1. Start Visual Studio.

    2. On the File menu, choose New, Project.

    3. On the New Project window, in the panel on the left, expand Installed, and then expand Visual C#.

    4. In the panel on the right, choose Console App (.NET Framework).

    5. For Name, type AmazonSESSample, and then choose OK.

  2. Use NuGet to include the Amazon SES packages in your solution by completing the following steps:

    1. In the Solution Explorer pane, right-click your project, and then choose Manage NuGet Packages.

    2. On the NuGet: AmazonSESSample tab, choose Browse.

    3. In the search box, type AWSSDK.SimpleEmail.

    4. Choose the AWSSDK.SimpleEmail package, and then choose Install.

    5. On the Preview Changes window, choose OK.

  3. On the Program.cs tab, paste the following code:

    using Amazon; using System; using System.Collections.Generic; using Amazon.SimpleEmail; using Amazon.SimpleEmail.Model; namespace AmazonSESSample { class Program { // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. static readonly string senderAddress = "sender@example.com"; // Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified. static readonly string receiverAddress = "recipient@example.com"; // The configuration set to use for this email. If you do not want to use a // configuration set, comment out the following property and the // ConfigurationSetName = configSet argument below. static readonly string configSet = "ConfigSet"; // The subject line for the email. static readonly string subject = "Amazon SES test (AWS SDK for .NET)"; // The email body for recipients with non-HTML email clients. static readonly string textBody = "Amazon SES Test (.NET)\r\n" + "This email was sent through Amazon SES " + "using the AWS SDK for .NET."; // The HTML body of the email. static readonly string htmlBody = @"<html> <head></head> <body> <h1>Amazon SES Test (AWS SDK for .NET)</h1> <p>This email was sent with <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the <a href='https://aws.amazon.com/sdk-for-net/'> AWS SDK for .NET</a>.</p> </body> </html>"; static void Main(string[] args) { // Replace USWest2 with the AWS Region you're using for Amazon SES. // Acceptable values are EUWest1, USEast1, and USWest2. using (var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USWest2)) { var sendRequest = new SendEmailRequest { Source = senderAddress, Destination = new Destination { ToAddresses = new List<string> { receiverAddress } }, Message = new Message { Subject = new Content(subject), Body = new Body { Html = new Content { Charset = "UTF-8", Data = htmlBody }, Text = new Content { Charset = "UTF-8", Data = textBody } } }, // If you are not using a configuration set, comment // or remove the following line ConfigurationSetName = configSet }; try { Console.WriteLine("Sending email using Amazon 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(); } } }
  4. In the code editor, do the following:

    • Replace sender@example.com with the "From:" email address. This address must be verified. For more information, see Verifying Identities in Amazon SES.

    • Replace recipient@example.com with the "To:" address. If your account is still in the sandbox, this address must also be verified.

    • Replace ConfigSet with the name of the configuration set to use when sending this email.

    • Replace USWest2 with the name of the AWS Region endpoint you use to send email using Amazon SES. The following values are allowed: USEast1, USWest2, and EUWest1.

    When you finish, save Program.cs.

  5. Build and run the application by completing the following steps:

    1. On the Build menu, choose Build Solution.

    2. On the Debug menu, choose Start Debugging. A console window appears.

  6. Review the output of the console. If the email was successfully sent, the console displays "The email was sent successfully."

  7. If the email was successfully sent, sign in to the email client of the recipient address. You will find the message that you sent.

On this page: