Menu
Amazon Simple Email Service
Developer Guide

Send an Email by Accessing the Amazon SES SMTP Interface Programmatically

You can access the Amazon SES SMTP interface by using an SMTP-enabled programming language. You provide the Amazon SES SMTP hostname and port number along with your SMTP credentials and then use the programming language's generic SMTP functions to send the email.

Use the Filter View menu above to select a programming language.

C#

The following procedure shows how to use Microsoft Visual Studio to create a C# console application that sends an email through Amazon SES. The procedures in this section apply to Visual Studio 2017, but the process of creating C# console applications is similar across Microsoft Visual Studio editions.

Before you perform the following procedure, complete the setup tasks described in Before You Begin with Amazon SES and Send an Email Through Amazon SES Using SMTP.

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.

To send an email using the Amazon SES SMTP interface with C#

  1. Create a console project in Visual Studio by performing the following steps:

    1. Open Microsoft Visual Studio.

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

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

    4. Under Visual C#, choose Windows Classic Desktop.

    5. On the menu at the top of the window, choose .NET Framework 4.5, as shown in the following image.

      
                            The .NET Framework selection menu on the New Project
                                window.

      Note

      You can select a later version of the .NET Framework if necessary.

    6. Choose Console App (.NET Framework).

    7. In the Name field, type AmazonSESSample.

    8. Choose OK.

  2. In your Visual Studio project, replace the entire contents of Program.cs with the following code:

    Copy
    using System; namespace AmazonSESSample { class Program { static void Main(string[] args) { // Replace with your "From" address. This address must be verified. const String FROM = "sender@example.com"; // Replace with a "To" address. If your account is still in the // sandbox, this address must be verified. const String TO = "recipient@example.com"; const String SUBJECT = "Amazon SES test (SMTP interface accessed using C#)"; const String BODY = "This email was sent through the Amazon SES SMTP interface " + "using C#."; // Supply your SMTP credentials below. Note that your SMTP // credentials are different from your AWS credentials. // Replace with your SMTP username. const String SMTP_USERNAME = "SMTP_USERNAME"; // Replace with your SMTP password. const String SMTP_PASSWORD = "SMTP_PASSWORD"; // Amazon SES SMTP host name. This example uses the US West // (Oregon) region. const String HOST = "email-smtp.us-west-2.amazonaws.com"; // The port you will connect to on the Amazon SES SMTP endpoint. We // are choosing port 587 because we will use STARTTLS to encrypt // the connection. const int PORT = 587; // Create an SMTP client with the specified host name and port. using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT)) { // Create a network credential with your SMTP user name // and password. client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD); // Use SSL when accessing Amazon SES. The SMTP session will // begin on an unencrypted connection, and then the client will // issue a STARTTLS command to upgrade to an encrypted // connection using SSL. client.EnableSsl = true; // Send the email. try { Console.WriteLine("Attempting to send email..."); client.Send(FROM, TO, SUBJECT, BODY); 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(); } } }
  3. In Program.cs, replace the following email addresses 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.

  4. In Program.cs, replace the following SMTP credentials with the values that you obtained in Obtaining Your Amazon SES SMTP Credentials:

    Important

    Your SMTP credentials are different from your AWS credentials. For more information about credentials, see Using Credentials With Amazon SES.

    • YOUR_SMTP_USERNAME—Replace with your SMTP username. Note that your SMTP username credential is a 20-character string of letters and numbers, not an intelligible name.

    • YOUR_SMTP_PASSWORD—Replace with your SMTP password.

  5. (Optional) If you want to use an Amazon SES SMTP endpoint in a Region other than US West (Oregon), change the value of the variable HOST to the endpoint you want to use. For a list of Amazon SES endpoints, see Regions and Amazon SES.

  6. Save Program.cs.

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

  8. To run the program, choose Debug and then choose Start Debugging.

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

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

Java

This example uses the Eclipse IDE and the JavaMail API to send email through Amazon SES using the SMTP interface.

Before you perform the following procedure, complete the setup tasks described in Before You Begin with Amazon SES and Send an Email Through Amazon SES Using SMTP.

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.

To send an email using the Amazon SES SMTP interface with Java

  1. In a web browser, go to the JavaMail Github page. Under Downloads, choose javax.mail.jar to download the latest version of JavaMail.

    Important

    This tutorial requires JavaMail version 1.5 or later.

  2. Create a project in Eclipse by performing the following steps:

    1. Start Eclipse.

    2. In Eclipse, choose File, choose New, and then choose Java Project.

    3. In the Create a Java Project dialog box, type a project name and then choose Next.

    4. In the Java Settings dialog box, choose the Libraries tab.

    5. Choose Add External JARs.

    6. Browse to the folder in which you downloaded JavaMail. Choose the file javax.mail.jar, and then choose Open.

    7. In the Java Settings dialog box, choose Finish.

  3. In Eclipse, in the Package Explorer window, expand your project.

  4. Under your project, right-click the src directory, choose New, and then choose Class.

  5. In the New Java Class dialog box, in the Name field, type AmazonSESSample and then choose Finish.

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

    Copy
    import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class AmazonSESSample { static final String FROM = "SENDER@EXAMPLE.COM"; // Replace with your "From" address. This address must be verified. static final String TO = "RECIPIENT@EXAMPLE.COM"; // Replace with a "To" address. If your account is still in the // sandbox, this address must be verified. static final String BODY = "This email was sent through the Amazon SES SMTP interface by using Java."; static final String SUBJECT = "Amazon SES test (SMTP interface accessed using Java)"; // Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials. static final String SMTP_USERNAME = "YOUR_SMTP_USERNAME"; // Replace with your SMTP username. static final String SMTP_PASSWORD = "YOUR_SMTP_PASSWORD"; // Replace with your SMTP password. // Amazon SES SMTP host name. This example uses the US West (Oregon) Region. static final String HOST = "email-smtp.us-west-2.amazonaws.com"; // The port you will connect to on the Amazon SES SMTP endpoint. We are choosing port 25 because we will use // STARTTLS to encrypt the connection. static final int PORT = 25; public static void main(String[] args) throws Exception { // Create a Properties object to contain connection configuration information. Properties props = System.getProperties(); props.put("mail.transport.protocol", "smtps"); props.put("mail.smtp.port", PORT); // Set properties indicating that we want to use STARTTLS to encrypt the connection. // The SMTP session will begin on an unencrypted connection, and then the client // will issue a STARTTLS command to upgrade to an encrypted connection. props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.starttls.required", "true"); // Create a Session object to represent a mail session with the specified properties. Session session = Session.getDefaultInstance(props); // Create a message with the specified information. MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(FROM)); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO)); msg.setSubject(SUBJECT); msg.setContent(BODY,"text/plain"); // Create a transport. Transport transport = session.getTransport(); // Send the message. try { System.out.println("Attempting to send an email through the Amazon SES SMTP interface..."); // Connect to Amazon SES using the SMTP username and password you specified above. transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD); // Send the email. transport.sendMessage(msg, msg.getAllRecipients()); System.out.println("Email sent!"); } catch (Exception ex) { System.out.println("The email was not sent."); System.out.println("Error message: " + ex.getMessage()); } finally { // Close and terminate the connection. transport.close(); } } }
  7. In AmazonSESSample.java, replace the following email addresses 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.

  8. In AmazonSESSample.java, replace the following SMTP credentials with the values that you obtained in Obtaining Your Amazon SES SMTP Credentials:

    Important

    Your SMTP credentials are different from your AWS credentials. For more information about credentials, see Using Credentials With Amazon SES.

    • YOUR_SMTP_USERNAME—Replace with your SMTP username credential. Note that your SMTP username credential is a 20-character string of letters and numbers, not an intelligible name.

    • YOUR_SMTP_PASSWORD—Replace with your SMTP password.

  9. (Optional) If you want to use an Amazon SES SMTP endpoint in a Region other than US West (Oregon), change the value of the variable HOST to the endpoint you want to use. For a list of Amazon SES endpoints, see Regions and Amazon SES.

  10. Save AmazonSESSample.java.

  11. To build the project, choose Project and then choose Build Project. (If this option is disabled, then you may have automatic building enabled.)

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

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

  14. Sign into the email client of the recipient address. You will find the message that you sent.

PHP

This example uses the PHPMailer package to send email through Amazon SES using the SMTP interface.

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 SMTP credentials—You need an Amazon SES SMTP user name and password to access the Amazon SES SMTP interface. Your SMTP credentials are not the same as your AWS credentials. You can find your SMTP credentials by going to the SMTP Settings page of the Amazon SES console. For more information about SMTP credentials, see Obtaining Your Amazon SES SMTP Credentials.

  • Install PHP—PHP is available at http://php.net/downloads.php. After you install PHP, add the path to PHP in your environment variables so that you can run PHP from any command prompt.

  • Install the Composer dependency manager—The Composer dependency manager will enable you to download and install the PHPMailer class and its dependencies. To install Composer, follow the installation instructions at https://getcomposer.org/download.

  • Install the PHPMailer package— Once you have installed Composer, open the file composer.json in a text editor. In the require section, add the following line: "phpmailer/phpmailer":"~5.2", and then save the file. At the command line, change to the directory that contains the composer.json file, and then type php composer.phar update to download and install PHPMailer.

Procedure

The following procedure shows how to send an email through Amazon SES with PHP.

To send an email using the Amazon SES SMTP interface with PHP

  1. Create a file named amazon-ses-smtp-sample.php. Open the file with a text editor and paste in the following code:

    Copy
    <?php // Modify the path in the require statement below to refer to the // location of your Composer autoload.php file. require './vendor/autoload.php'; // Instantiate a new PHPMailer $mail = new PHPMailer; // Tell PHPMailer to use SMTP $mail->isSMTP(); // If you're using Amazon SES in a Region other than US West (Oregon), // replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP // endpoint in the appropriate Region. $mail->Host = 'email-smtp.us-west-2.amazonaws.com'; // Tells PHPMailer to use SMTP authentication $mail->SMTPAuth = true; // Replace smtp_username with your Amazon SES SMTP user name. $mail->Username = 'smtp_username'; // Replace smtp_password with your Amazon SES SMTP password. $mail->Password = 'smtp_password'; // Enable SSL encryption $mail->SMTPSecure = 'ssl'; // The port you will connect to on the Amazon SES SMTP endpoint. $mail->Port = 465; // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. $mail->setFrom('sender@example.com', 'Sender Name'); // Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified. // Also note that you can include several addAddress() lines to send // email to multiple recipients. $mail->addAddress('recipient@example.com', 'Recipient Name'); // Tells PHPMailer to send HTML-formatted email $mail->isHTML(true); // The subject line of the email $mail->Subject = 'Amazon SES test (SMTP interface accessed using PHP)'; // The HTML-formatted body of the email $mail->Body = '<h1>Email Test</h1> <p>This email was sent through the <a href="http://aws.amazon.com/ses/">Amazon SES</a> SMTP interface using the <a href="https://github.com/PHPMailer/PHPMailer"> PHPMailer</a> class.</p>'; // The alternative email body; this is only displayed when a recipient // opens the email in a non-HTML email client. The \r\n represents a // line break. $mail->AltBody = "Email Test\r\nThis email was sent through the Amazon SES SMTP interface using the PHPMailer class."; if(!$mail->send()) { echo 'Email not sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Email sent!'; }
  2. In amazon-ses-smtp-sample.php, 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.

    • smtp_username—Replace with your SMTP user name credential, which you obtained from the SMTP Settings page of the Amazon SES console. This is not the same as your AWS access key ID. Note that your SMTP user name credential is a 20-character string of letters and numbers, not an intelligible name.

    • smtp_password—Replace with your SMTP password, which you obtained from the SMTP Settings page of the Amazon SES console. This is not the same as your AWS secret access key.

    • (Optional) email-smtp.us-west-2.amazonaws.com—If you want to use an Amazon SES SMTP endpoint in a Region other than US West (Oregon), replace this with the Amazon SES SMTP endpoint in the Region you want to use. For a list of Amazon SES SMTP endpoints, see Regions and Amazon SES.

  3. Save amazon-ses-smtp-sample.php.

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

  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.