Menu
Amazon Simple Email Service
Developer Guide

Send an Email through Amazon SES Programmatically using an AWS SDK

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.

Use the Filter View menu above to select an SDK.

Note

If you have not already done so, complete the prerequisites listed in Send an Email Through Amazon SES Using an AWS SDK before you attempt to complete the tutorials in this section.

AWS SDK for .NET

The following procedure shows you how to use Visual Studio and AWS Toolkit for Visual Studio to create an AWS SDK project and modify the C# code to send an email through Amazon SES. These procedures use the latest version of Microsoft Visual Studio (Visual Studio 2017), but the process of creating a new project based on a template is similar across Visual Studio releases.

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. 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.

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 Verification Procedures.

  • 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/. This sample project was tested using Visual Studio 2017 Community Edition (version 15.2).

  • Install the AWS Toolkit for Visual Studio—Instructions for adding the AWS Toolkit for Visual Studio are available at http://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/getting-set-up.html.

    Note

    Installing the Toolkit for Visual Studio will also install the AWS SDK for .NET, if it is not already installed.

  • 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 an Amazon SES sample 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, expand Visual C#, and then expand AWS Samples.

    4. Under AWS Samples, choose App Services.

    5. In the panel on the right, choose AWS SES Sample.

    6. On the .NET Framework menu at the top of the window, choose .NET Framework 4.6.1.

    7. In the Name field, type AmazonSESSample, and then choose OK.

  2. On the AWS Access Credentials window, choose the account profile and Region you want to use for this example, and then choose OK.

  3. In Solution Explorer, double-click Program.cs.

  4. In Program.cs, change the value of senderAddress to a verified email address. Enclose the email address in quotes.

  5. In Program.cs, change the value of receiverAddress to a verified email address. Enclose the email address in quotes.

  6. Save Program.cs.

  7. 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.

  8. Review the output of the console. If the email was successfully sent, the console displays "The email was sent successfully." Otherwise, it displays an error message.

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

AWS SDK for Java

The following procedure shows you how to use Eclipse IDE for Java EE Developers and AWS Toolkit for Eclipse to create an AWS SDK project and modify the Java code to send an email through Amazon SES.

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. 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.

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 Verification Procedures.

  • 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 Using Credentials With Amazon SES.

  • Install Eclipse—Eclipse is available at https://www.eclipse.org/downloads. The code in this tutorial was tested using Eclipse Neon.3 (version 4.6.3), running version 1.8 of the Java Runtime Environment.

  • Install the AWS Toolkit for Eclipse—Instructions for adding the AWS Toolkit for Eclipse to your Eclipse installation are available at https://aws.amazon.com/eclipse. The code in this tutorial was tested using version 2.3.1 of the AWS Toolkit for Eclipse.

  • 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 Java.

To send an email using the AWS SDK for Java

  1. Create an AWS Java Project in Eclipse by performing the following steps:

    1. Start Eclipse.

    2. On the File menu, choose New, and then choose Other. On the New window, expand the AWS folder, and then choose AWS Java Project.

    3. In the New AWS Java Project dialog box, do the following:

      1. For Project name, type a project name.

      2. Under AWS SDK for Java Samples, select Amazon Simple Email Service JavaMail Sample.

      3. Choose Finish.

  2. In Eclipse, in the Package Explorer pane, expand your project.

  3. Under your project, expand the src/main/java folder, expand the com.amazon.aws.samples folder, and then double-click AmazonSESSample.java.

  4. Replace the entire contents of AmazonSESSample.java with the following code:

    Copy
    package com.amazonaws.samples; import java.io.IOException; import com.amazonaws.services.simpleemail.*; import com.amazonaws.services.simpleemail.model.*; import com.amazonaws.regions.*; public class AmazonSESSample { // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. static final String FROM = "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 final String TO = "recipient@example.com"; // The configuration set to use for this email. If you do not want to use a // configuration set, comment the next line and line 60. static final String CONFIGSET = "ConfigSet"; // The subject line for the email. static final String SUBJECT = "Amazon SES test (AWS SDK for Java)"; // The HTML body for the email. static final String HTMLBODY = "<h1>Amazon SES test (AWS SDK for Java)</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-java/'>" + "AWS SDK for Java</a>"; // The email body for recipients with non-HTML email clients. static final String TEXTBODY = "This email was sent through Amazon SES " + "using the AWS SDK for Java."; // The subject line for the email. static final String SUBJECT = "Amazon SES test (AWS SDK for Java)"; public static void main(String[] args) throws IOException { try { AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard() // Replace US_WEST_2 with the AWS Region you're using for // Amazon SES. .withRegion(Regions.US_WEST_2).build(); SendEmailRequest request = new SendEmailRequest() .withDestination( new Destination().withToAddresses(TO)) .withMessage(new Message() .withBody(new Body() .withHtml(new Content() .withCharset("UTF-8").withData(HTMLBODY)) .withText(new Content() .withCharset("UTF-8").withData(TEXTBODY))) .withSubject(new Content() .withCharset("UTF-8").withData(SUBJECT))) .withSource(FROM) // Comment or remove the next line if you are not using a // configuration set .withConfigurationSetName(CONFIGSET); client.sendEmail(request); System.out.println("Email sent!"); } catch (Exception ex) { System.out.println("The email was not sent. Error message: " + ex.getMessage()); } } }
  5. In AmazonSESSample.java, 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.

    • SENDER@EXAMPLE.COM—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.

    • RECIPIENT@EXAMPLE.COM—Replace with your "To" email address. If your account is still in the sandbox, you must verify this address before you use it. For more information, see Moving Out of the Amazon SES Sandbox.

    • US_WEST_2—Set this to the AWS Region of the Amazon SES endpoint you want to connect to. Note that your sandbox 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 West (Oregon) Region. Examples of other Regions that Amazon SES supports are US_EAST_1 and EU_WEST_1. For a complete list of AWS Regions that Amazon SES supports, see Regions and Amazon SES.

  6. Save AmazonSESSample.java.

  7. To build the project, choose Project and then choose Build Project.

    Note

    If this option is disabled, automatic building may be enabled; if so, skip this step.

  8. To start the program and send the email, choose Run and then choose Run again.

  9. Review the output of the console pane in Eclipse. If the email was successfully sent, the console displays "Email sent!" Otherwise, it displays an error message.

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

AWS SDK for PHP

This topic shows how to use the AWS SDK for PHP to send an email through Amazon SES.

Important

In this 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 Amazon SES Email Sending.

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 Verification Procedures.

  • 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 PHP—PHP is available at http://php.net/downloads.php. This tutorial requires PHP version 5.5 or higher. After you install PHP, add the path to PHP in your environment variables so that you can run PHP from any command prompt. The code in this tutorial was tested using PHP 7.0.20.

  • Install the AWS SDK for PHP version 3—For download and installation instructions, see the AWS SDK for PHP documentation. The code in this tutorial was tested using version 3.31.0 of the SDK.

  • 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 PHP.

To send an email through Amazon SES using the AWS SDK for PHP

  1. In a text editor, create a file named amazon-ses-sample.php. Paste the following code:

    Copy
    <?php // Replace path_to_sdk_inclusion with the path to the SDK as described in // http://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/basic-usage.html define('REQUIRED_FILE','path_to_sdk_inclusion'); // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. define('SENDER', 'sender@example.com'); // Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified. define('RECIPIENT', 'recipient@example.com'); // Specify a configuration set. If you do not want to use a configuration // set, comment the next line, and line 67. define('CONFIGSET','ConfigSet'); // Replace us-west-2 with the AWS Region you're using for Amazon SES. define('REGION','us-west-2'); define('SUBJECT','Amazon SES test (AWS SDK for PHP)'); define('HTMLBODY','<h1>AWS Amazon Simple Email Service Test Email</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-php/">'. 'AWS SDK for PHP</a>.</p>'); define('TEXTBODY','This email was send with Amazon SES using the AWS SDK for PHP.'); define('CHARSET','UTF-8'); require REQUIRED_FILE; use Aws\Ses\SesClient; use Aws\Ses\Exception\SesException; $client = SesClient::factory(array( 'version'=> 'latest', 'region' => REGION )); try { $result = $client->sendEmail([ 'Destination' => [ 'ToAddresses' => [ RECIPIENT, ], ], 'Message' => [ 'Body' => [ 'Html' => [ 'Charset' => CHARSET, 'Data' => HTMLBODY, ], 'Text' => [ 'Charset' => CHARSET, 'Data' => TEXTBODY, ], ], 'Subject' => [ 'Charset' => CHARSET, 'Data' => SUBJECT, ], ], 'Source' => SENDER, // Comment or remove the following line if you are not using a configuration set 'ConfigurationSetName' => CONFIGSET, ]); $messageId = $result->get('MessageId'); echo("Email sent! Message ID: $messageId"."\n"); } catch (SesException $error) { echo("The email was not sent. Error message: ".$error->getAwsErrorMessage()."\n"); } ?>
  2. In amazon-ses-sample.php, replace the following with your own values:

    • path_to_sdk_inclusion—Replace with the path required to include the AWS SDK for PHP in the program. For more information, see the AWS SDK for PHP documentation.

    • sender@example.com—Replace with an email address that you have verified with Amazon SES. For more information, see Verifying Email Addresses and Domains. Email addresses in Amazon SES are case-sensitive. Make sure that the address you enter is exactly the same as the one you verified.

    • recipient@example.com—Replace with the address of the recipient. If your account is still in the sandbox, you must verify this address before you use it. For more information, see Moving Out of the Amazon SES Sandbox. Make sure that the address you enter is exactly the same as the one you verified.

    • (Optional) us-west-2—If you want to use Amazon SES in a Region other than US West (Oregon), replace this with the Region you want to use. For a list of Regions in which Amazon SES is available, see Regions and Amazon SES.

  3. Save amazon-ses-sample.php.

  4. To run the program, open a command prompt in the same directory as amazon-ses-sample.php, and then type php amazon-ses-sample.php

  5. Review the output. If the email was successfully sent, the console displays "Email sent!" Otherwise, it displays an error message.

    Note

    If you encounter a "cURL error 60: SSL certificate problem" error when you run the program, download the latest CA bundle as described in the AWS SDK for PHP documentation. Then, in amazon-ses-sample.php, add the following lines to the SesClient::factory array, replace path_of_certs with the path to the CA bundle you downloaded, and re-run the program.

    Copy
    'http' => [ 'verify' => 'path_of_certs\ca-bundle.crt' ]
  6. Sign in to the email client of the recipient address. You will find the message that you sent.

AWS SDK for Ruby

This topic shows how to use the AWS SDK for Ruby to send an email through Amazon SES.

Important

In this 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 Amazon SES Email Sending.

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 Verification Procedures.

  • 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 Ruby—Ruby is available at https://www.ruby-lang.org/en/downloads/. The code in this tutorial was tested using Ruby 1.9.3. After you install Ruby, add the path to Ruby in your environment variables so that you can run Ruby from any command prompt.

  • Install the AWS SDK for Ruby—For download and installation instructions, see Installing the AWS SDK for Ruby in the AWS SDK for Ruby Developer Guide. The sample code in this tutorial was tested using version 2.9.36 of the AWS SDK for Ruby.

  • 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 Ruby.

To send an email through Amazon SES using the AWS SDK for Ruby

  1. In a text editor, create a file named amazon-ses-sample.rb. Paste the following code into the file:

    Copy
    require 'aws-sdk' # Replace sender@example.com with your "From" address. # This address must be verified with Amazon SES. sender = "sender@example.com" # Replace recipient@example.com with a "To" address. If your account # is still in the sandbox, this address must be verified. recipient = "recipient@example.com" # Specify a configuration set. If you do not want to use a configuration # set, comment the next line and line 65. configsetname = "ConfigSet" # Replace us-west-2 with the AWS Region you're using for Amazon SES. awsregion = "us-west-2" # The subject line for the email. subject = "Amazon SES test (AWS SDK for Ruby)" # The HTML body of the email. htmlbody = '<h1>Amazon SES test (AWS SDK for Ruby)</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-ruby/">'\ 'AWS SDK for Ruby</a>.' # The email body for recipients with non-HTML email clients. textbody = "This email was sent with Amazon SES using the AWS SDK for Ruby." # Specify the text encoding scheme. encoding = "UTF-8" # Create a new SES resource and specify a region ses = Aws::SES::Client.new(region: awsregion) # Try to send the email. begin # Provide the contents of the email. resp = ses.send_email({ destination: { to_addresses: [ recipient, ], }, message: { body: { html: { charset: encoding, data: htmlbody, }, text: { charset: encoding, data: textbody, }, }, subject: { charset: encoding, data: subject, }, }, source: sender, // Comment or remove the following line if you are not using a configuration set configuration_set_name: configsetname, }) puts "Email sent!" # If something goes wrong, display an error message. rescue Aws::SES::Errors::ServiceError => error puts "Email not sent. Error message: #{error}" end
  2. In amazon-ses-sample.rb, replace the following with your own values:

    • sender@example.com—Replace with an email address that you have verified with Amazon SES. For more information, see Verifying Email Addresses and Domains. Email addresses in Amazon SES are case-sensitive. Make sure that the address you enter is exactly the same as the one you verified.

    • recipient@example.com—Replace with the address of the recipient. If your account is still in the sandbox, you must verify this address before you use it. For more information, see Moving Out of the Amazon SES Sandbox. Make sure that the address you enter is exactly the same as the one you verified.

    • (Optional) us-west-2—If you want to use Amazon SES in a region other than US West (Oregon), replace this with the region you want to use. For a list of regions in which Amazon SES is available, see Regions and Amazon SES.

  3. Save amazon-ses-sample.rb.

  4. To run the program, open a command prompt in the same directory as amazon-ses-sample.rb, and type ruby amazon-ses-sample.rb

  5. Review the output. If the email was successfully sent, the console displays "Email sent!" Otherwise, it displays an error message.

  6. Sign in to the email client of the recipient address. You will find the message that you sent.

AWS SDK for Python (Boto)

This topic shows how to use the AWS SDK for Python (Boto) to send an email through Amazon SES.

Important

In this 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 Amazon SES Email Sending.

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 Verification Procedures.

  • 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 Python—Python is available at https://www.python.org/downloads/. The code in this tutorial was tested using Python 2.7.6 and Python 3.6.1. After you install Python, add the path to Python in your environment variables so that you can run Python from any command prompt.

  • Install the AWS SDK for Python (Boto)—For download and installation instructions, see the AWS SDK for Python (Boto) documentation. The sample code in this tutorial was tested using version 1.4.4 of the SDK for Python.

  • 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 SDK for Python.

To send an email through Amazon SES using the SDK for Python

  1. In a text editor, create a file named amazon-ses-sample.py. Paste the following code into the file:

    Copy
    import boto3 from botocore.exceptions import ClientError # Replace sender@example.com with your "From" address. # This address must be verified with Amazon SES. sender = "sender@example.com" # Replace recipient@example.com with a "To" address. If your account # is still in the sandbox, this address must be verified. recipient = "recipient@example.com" # (Optional) the name of a configuration set to use for this message. # If you comment out this line, you also need to remove or comment out # the "X-SES-CONFIGURATION-SET" header on line 64, below. configSet = "ConfigSet" # If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES. awsregion = "us-west-2" # The subject line for the email. subject = "Amazon SES Test (SDK for Python)" # The HTML body of the email. htmlbody = """<h1>Amazon SES Test (SDK for Python)</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-python/'>AWS SDK for Python (Boto)</a>.</p>""" # The email body for recipients with non-HTML email clients. textbody = "This email was sent with Amazon SES using the AWS SDK for Python (Boto)" # The character encoding for the email. charset = "UTF-8" # Create a new SES resource and specify a region. client = boto3.client('ses',region_name=awsregion) # Try to send the email. try: #Provide the contents of the email. response = client.send_email( Destination={ 'ToAddresses': [ recipient, ], }, Message={ 'Body': { 'Html': { 'Charset': charset, 'Data': htmlbody, }, 'Text': { 'Charset': charset, 'Data': textbody, }, }, 'Subject': { 'Charset': charset, 'Data': subject, }, }, Source=sender, # Comment or delete the next line if you are not using a configuration set ConfigurationSetName=configSet, ) # Display an error if something goes wrong. except ClientError as e: print(e.response['Error']['Message']) else: print("Email sent! Message ID:"), print(response['ResponseMetadata']['RequestId'])
  2. In amazon-ses-sample.py, replace the following with your own values:

    • sender@example.com—Replace with an email address that you have verified with Amazon SES. For more information, see Verifying Email Addresses and Domains. Email addresses in Amazon SES are case-sensitive. Make sure that the address you enter is exactly the same as the one you verified.

    • recipient@example.com—Replace with the address of the recipient. If your account is still in the sandbox, you must verify this address before you use it. For more information, see Moving Out of the Amazon SES Sandbox. Make sure that the address you enter is exactly the same as the one you verified.

    • (Optional) us-west-2—If you want to use Amazon SES in a region other than US West (Oregon), replace this with the region you want to use. For a list of regions in which Amazon SES is available, see Regions and Amazon SES.

  3. Save amazon-ses-sample.py.

  4. To run the program, open a command prompt in the same directory as amazon-ses-sample.py, and then type python amazon-ses-sample.py

  5. Review the output. If the email was successfully sent, the console displays "Email sent!" Otherwise, it displays an error message.

  6. Sign in to the email client of the recipient address. You will find the message that you sent.