Menu
Amazon Simple Email Service
Developer Guide

Sending Email using AWS SDKs

The AWS SDKs contain built-in methods for interacting with Amazon SES and several other AWS services. If you plan to use Amazon SES along with other AWS services, we recommend that you use an SDK. To learn more about the AWS SDKs, see Tools for Amazon Web Services

In this section, you will find code examples in several programming languages that demonstrate the process of sending email through Amazon SES using the AWS SDKs.

C#GoJavaJavaScriptPHPPythonRuby
C#

The following code example is a complete solution for sending email through Amazon SES using the AWS SDK for .NET. This code example assumes that you have installed the AWS SDK for .NET, and that you have created a shared credentials file. For more information about creating a shared credentials file, see Create a Shared Credentials File.

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(); } } }
Go

The following code example is a complete solution for sending email through Amazon SES using the AWS SDK for Go. This code example assumes that you have installed the AWS SDK for Go, and that you have created a shared credentials file. For more information about creating a shared credentials file, see Create a Shared Credentials File.

package main import ( "fmt" //go get -u github.com/aws/aws-sdk-go "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ses" "github.com/aws/aws-sdk-go/aws/awserr" ) const ( // 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 out the following constant and the // ConfigurationSetName: aws.String(ConfigurationSet) argument below ConfigurationSet = "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 Go)" // The HTML body for the email. HtmlBody = "<h1>Amazon SES Test Email (AWS SDK for Go)</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-go/'>AWS SDK for Go</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 Go." // The character encoding for the email. CharSet = "UTF-8" ) func main() { // Create a new session and specify an AWS Region. sess, err := session.NewSession(&aws.Config{ Region:aws.String(AwsRegion)}, ) // Create an SES client in the session. svc := ses.New(sess) // Assemble the email. input := &ses.SendEmailInput{ Destination: &ses.Destination{ CcAddresses: []*string{ }, ToAddresses: []*string{ aws.String(Recipient), }, }, Message: &ses.Message{ Body: &ses.Body{ Html: &ses.Content{ Charset: aws.String(CharSet), Data: aws.String(HtmlBody), }, Text: &ses.Content{ Charset: aws.String(CharSet), Data: aws.String(TextBody), }, }, Subject: &ses.Content{ Charset: aws.String(CharSet), Data: aws.String(Subject), }, }, Source: aws.String(Sender), // Comment or remove the following line if you are not using a configuration set ConfigurationSetName: aws.String(ConfigurationSet), } // Attempt to send the email. result, err := svc.SendEmail(input) // Display error messages if they occur. if err != nil { if aerr, ok := err.(awserr.Error); ok { switch aerr.Code() { case ses.ErrCodeMessageRejected: fmt.Println(ses.ErrCodeMessageRejected, aerr.Error()) case ses.ErrCodeMailFromDomainNotVerifiedException: fmt.Println(ses.ErrCodeMailFromDomainNotVerifiedException, aerr.Error()) case ses.ErrCodeConfigurationSetDoesNotExistException: fmt.Println(ses.ErrCodeConfigurationSetDoesNotExistException, aerr.Error()) default: fmt.Println(aerr.Error()) } } else { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) } return } fmt.Println("Email Sent!") fmt.Println(result) }
Java

The following code example is a complete solution for sending email through Amazon SES using the AWS SDK for Java. This code example assumes that you have installed the AWS SDK for Java, and that you have created a shared credentials file. For more information about creating a shared credentials file, see Create a Shared Credentials File.

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 following variable and the // .withConfigurationSetName(CONFIGSET); argument below. 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."; 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()); } } }
JavaScript

The following code example is a complete solution for sending email through Amazon SES using the AWS SDK for JavaScript in Node.js. This code example assumes that you have installed the SDK for JavaScript in Node.js. You must also create a configuration file that contains your AWS Access Key ID, Secret Access Key, and preferred AWS Region. For more information about creating this file, see Loading Credentials in Node.js from a JSON File.

'use strict'; var aws = require('aws-sdk'); // Provide the full path to your config.json file. aws.config.loadFromPath('./config.json'); // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. const sender = "Sender Name <sender@recipient.com>"; // Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified. const recipient = "recipient@example.com"; // Specify a configuration set. If you do not want to use a configuration // set, comment the following variable, and the // ConfigurationSetName : configuration_set argument below. const configuration_set = "ConfigSet"; // The subject line for the email. const subject = "Amazon SES Test (AWS SDK for JavaScript in Node.js)"; // The email body for recipients with non-HTML email clients. const body_text = "Amazon SES Test (SDK for JavaScript in Node.js)\r\n" + "This email was sent with Amazon SES using the " + "AWS SDK for JavaScript in Node.js."; // The HTML body of the email. const body_html = `<html> <head></head> <body> <h1>Amazon SES Test (SDK for JavaScript in Node.js)</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-node-js/'> AWS SDK for JavaScript in Node.js</a>.</p> </body> </html>`; // The character encoding for the email. const charset = "UTF-8"; // Create a new SES object. var ses = new aws.SES(); // Specify the parameters to pass to the API. var params = { Source: sender, Destination: { ToAddresses: [ recipient ], }, Message: { Subject: { Data: subject, Charset: charset }, Body: { Text: { Data: body_text, Charset: charset }, Html: { Data: body_html, Charset: charset } } }, ConfigurationSetName: configuration_set }; //Try to send the email. ses.sendEmail(params, function(err, data) { // If something goes wrong, print an error message. if(err) { console.log(err.message); } else { console.log("Email sent! Message ID: ", data.MessageId); } });
PHP

The following code example is a complete solution for sending email through Amazon SES using the AWS SDK for PHP. This code example assumes that you have installed the AWS SDK for PHP, and that you have created a shared credentials file. For more information about creating a shared credentials file, see Create a Shared Credentials File.

<?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 following variable, and the // 'ConfigurationSetName' => CONFIGSET argument below. 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, // If you are not using a configuration set, comment or delete the // following line '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"); } ?>
Python

The following code example is a complete solution for sending email through Amazon SES using the AWS SDK for Python (Boto). This code example assumes that you have installed the AWS SDK for Python (Boto), and that you have created a shared credentials file. For more information about creating a shared credentials file, see Create a Shared Credentials File.

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 Name <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 following variable, and the # ConfigurationSetName=CONFIGURATION_SET argument below. CONFIGURATION_SET = "ConfigSet" # If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES. AWS_REGION = "us-west-2" # The subject line for the email. SUBJECT = "Amazon SES Test (SDK for Python)" # The email body for recipients with non-HTML email clients. BODY_TEXT = ("Amazon SES Test (Python)\r\n" "This email was sent with Amazon SES using the " "AWS SDK for Python (Boto)." ) # The HTML body of the email. BODY_HTML = """<html> <head></head> <body> <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> </body> </html> """ # The character encoding for the email. CHARSET = "UTF-8" # Create a new SES resource and specify a region. client = boto3.client('ses',region_name=AWS_REGION) # 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': BODY_HTML, }, 'Text': { 'Charset': CHARSET, 'Data': BODY_TEXT, }, }, 'Subject': { 'Charset': CHARSET, 'Data': SUBJECT, }, }, Source=SENDER, # If you are not using a configuration set, comment or delete the # following line ConfigurationSetName=CONFIGURATION_SET, ) # 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'])
Ruby

The following code example is a complete solution for sending email through Amazon SES using the AWS SDK for Ruby. This code example assumes that you have installed the AWS SDK for Ruby, and that you have created a shared credentials file. For more information about creating a shared credentials file, see Create a Shared Credentials File.

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 following variable and the # configuration_set_name: configsetname argument below. 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

For technical discussions about various Amazon SES topics, visit the Amazon SES Blog. To browse and post questions, go to the Amazon SES Forum.