You can use an AWS SDK to send email through Amazon SES if you want to call the Amazon SES API, but you do not want to handle low-level details such as assembling and parsing HTTP requests and responses.
Before you send email using an AWS SDK, review the instructions in Before You Begin with Amazon SES (p. 18). For this tutorial, you also need to:
• Download an AWS SDK—Download and install an AWS SDK for either .NET or Java. For more information, see Downloading an AWS SDK (p. 43).
• Get your AWS credentials—To access Amazon SES programmatically, you need your AWS access keys. For more information, see Getting Your AWS Access Keys (p. 43).
After you have installed the appropriate SDK and retrieved your AWS credentials, you can send an email through Amazon SES using one of the following examples:
• Sending an Email Through Amazon SES Using AWS SDK for .NET (p. 28)
• Sending an Email Through Amazon SES Using AWS SDK for Java (p. 31) Amazon Simple Email Service Developer Guide Configuring Your Existing Email Server or Application
Sending an Email Through Amazon SES Using AWS SDK for .NET
The following procedure shows you how to use Microsoft Visual Studio and AWS Toolkit for Microsoft Visual Studio to create an AWS SDK project and modify the C# code to send an email through Amazon SES. The process to create a new project based on a project template is similar across Microsoft Visual Studio editions, but we'll go through the procedure using Microsoft Visual Studio Professional 2012.
Before you begin this procedure, complete the setup tasks described in Before You Begin with Amazon SES (p. 18) and Sending an Email Through Amazon SES Using an AWS SDK (p. 27).
Important
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 whenever possible. 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 Amazon SES Email Sending (p. 118).
To send an email using the AWS SDK for .NET v.2
1. Create an AWS project in Visual Studio by performing the following steps:
a. Open Visual Studio.
b. Click File, click New, and then click Project.
c. In the New Project dialog box, in the left pane, expand Installed, expand Templates, and then expand Visual C#.
d. Under Visual C#, click AWS.
e. Click AWS Empty Project.
f. In the Name field, type Amazon_SES_SDK_Test. The dialog box should look similar to the following figure.
g. Click OK.
Amazon Simple Email Service Developer Guide Sending an Email Using AWS SDK for .NET
2. In the AWS Access Credentials dialog box, select an existing account or enter the following information:
• Display Name—Type a name that identifies your account. Next time you create an AWS project in Visual Studio, you will be able to select this account so you do not have to enter the information again.
• Access Key ID—Enter the AWS access key ID that you obtained in Getting Your AWS Access Keys (p. 43).
• Secret Access Key—Enter the AWS secret access key that you obtained in Getting Your AWS Access Keys (p. 43).
• Account Number—(Optional) Enter your AWS account number. To find your AWS account number, go to the Security Credentials page in the AWS Management Console and click Account Identifiers.
(If you are not logged into your AWS account, this link will take you to an AWS account sign-in page first.) At the bottom of the page, under Account Identifiers, you will see your AWS Account ID.
• Default Region—Select the AWS region of the Amazon SES endpoint you want to connect to.
Note that your production access status, sending limits, and Amazon SES identity-related settings are specific to a given AWS region, so be sure to select an AWS region in which you set up Amazon SES. For a list of AWS regions that Amazon SES supports, see Regions and Amazon SES (p. 125).
3. Click OK.
Note
Your Access Key ID and Secret Access Key are added to the App.config file within your project. When you instantiate an AmazonSimpleEmailServiceClient object with no parameters, the constructor looks in App.config for your security credentials by default.
4. In your Visual Studio project, replace the entire contents of Program.cs with the following code:
using System;
using System.Collections.Generic;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;
Amazon Simple Email Service Developer Guide Sending an Email Using AWS SDK for .NET
namespace Amazon_SES_SDK_Test "From" address. This address must be verified.
const String TO = "[email protected]"; // Replace with a
"To" address. If you have not yet requested
// production access, this address must be verified.
const String SUBJECT = "Amazon SES test (AWS SDK for .NET)";
const String BODY = "This email was sent through Amazon SES by using the AWS SDK for .NET.";
// Construct an object to contain the recipient address.
SendEmailRequest request = new SendEmailRequest(FROM, destination, message);
// Choose the AWS region of the Amazon SES endpoint you want to connect to. Note that your production
// access status, sending limits, and Amazon SES identity-related settings are specific to a given
// AWS region, so be sure to select an AWS region in which you set up Amazon SES. Here, we are using
// the US East (N. Virginia) region. Examples of other regions that Amazon SES supports are USWest2
// and EUWest1. For a complete list, see ht
tp://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html
Amazon.RegionEndpoint REGION = Amazon.RegionEndpoint.USEast1;
// Instantiate an Amazon SES client, which will make the service call. Since we are instantiating an
// AmazonSimpleEmailServiceClient object with no parameters, the constructor looks in App.config for
// your AWS credentials by default. When you created your new AWS project in Visual Studio, the AWS
// credentials you entered were added to App.config.
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailSer viceClient(REGION);
// Send the email.
try {
Amazon Simple Email Service Developer Guide Sending an Email Using AWS SDK for .NET
Console.WriteLine("Attempting to send an email through Amazon SES by using the AWS SDK for .NET...");
client.SendEmail(request);
Console.WriteLine("Email sent!");
}
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();
} } }
5. In Program.cs, replace the following with your own values:
Important
The email addresses are case-sensitive. Make sure that the addresses are exactly the same as the ones you verified.
• [email protected]—Replace with your "From" email address.You must verify this address before you run this program. For more information, see Verifying Email Addresses and Domains in Amazon SES (p. 37).
• [email protected]—Replace with your "To" email address. If you have not yet requested production access, you must verify this address before you use it. For more information, see Requesting Production Access to Amazon SES (p. 44).
• REGION—Set this to the AWS region of the Amazon SES endpoint you want to connect to. Note that your production access status, sending limits, and Amazon SES identity-related settings are specific to a given AWS region, so be sure to select an AWS region in which you set up Amazon SES. In this example, we are using the US East (N. Virginia) region. Examples of other regions that Amazon SES supports are USWest2 and EUWest1. For a complete list of AWS regions that Amazon SES supports, see Regions and Amazon SES (p. 125).
6. Save Program.cs.
7. To build the project, click Build and then click Build Solution.
8. To run the program, click Debug and then click Start Debugging.
9. Review the program's console output to verify that the sending was successful. (You should see
"Email sent!")
10. Log into the email client of the recipient address. You should find the email message that you sent.