You can use an AWS SDK to send email through Amazon SES. AWS SDKs are available for several
programming languages. For more information, see Tools for Amazon Web Services
Prerequisites
The following prerequisites must be completed in order to complete any of the code samples in the next section:
-
If you haven't already done so, complete the tasks in Setting up Amazon Simple Email Service.
-
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. We recommend you use the Amazon SES console to verify email addresses. For more information, see Creating an email address identity.
-
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 in the AWS Management Console. For more information about credentials, see Types of Amazon SES credentials. -
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 Creating a shared credentials file to use when sending email through Amazon SES using an AWS SDK.
Code
examples
Important
In the following tutorials, 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 Using the mailbox simulator manually.
Select a programing language to view the example for that language:
The following procedure shows you how to send an email through Amazon SES using
Visual Studio
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.
Before you begin, perform the following tasks:
-
Install Visual Studio—Visual Studio is available at https://www.visualstudio.com/
.
To send an email using the AWS SDK for .NET
-
Create a new project by performing the following steps:
-
Start Visual Studio.
-
On the File menu, choose New, Project.
-
On the New Project window, in the panel on the left, expand Installed, and then expand Visual C#.
-
In the panel on the right, choose Console App (.NET Framework).
-
For Name, type
AmazonSESSample
, and then choose OK.
-
-
Use NuGet to include the Amazon SES packages in your solution by completing the following steps:
-
In the Solution Explorer pane, right-click your project, and then choose Manage NuGet Packages.
-
On the NuGet: AmazonSESSample tab, choose Browse.
-
In the search box, type
AWSSDK.SimpleEmail
. -
Choose the AWSSDK.SimpleEmail package, and then choose Install.
-
On the Preview Changes window, choose OK.
-
-
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 (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(); } } } -
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 Verified 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. For a list of regions where Amazon SES is available, see Amazon Simple Email Service (Amazon SES) in the AWS General Reference.
When you finish, save
Program.cs
. -
-
Build and run the application by completing the following steps:
-
On the Build menu, choose Build Solution.
-
On the Debug menu, choose Start Debugging. A console window appears.
-
-
Review the output of the console. If the email was successfully sent, the console displays "
The email was sent successfully.
" -
If the email was successfully sent, sign in to the email client of the recipient address. You will see the message that you sent.