Menu
Amazon Simple Email Service
Developer Guide (API Version 2010-12-01)

Send an Email Through the Amazon SES SMTP Interface with C#

The following procedure shows you how to use Microsoft Visual Studio to create a console application and modify the C# code to send an email through Amazon SES. The process to create a new project based on a project template that is similar across Microsoft Visual Studio editions, but we'll go through the procedure using Microsoft Visual Studio Professional 2012.

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. Choose File, choose New, and then choose Project.

    3. In the New Project dialog box, in the left pane, expand Installed, expand Templates, and then expand Visual C#.

    4. Under Visual C#, choose Windows.

    5. Choose Console Application.

    6. In the Name field, type AmazonSESSample. The dialog box should look similar to the following figure.

    7. 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) { const String FROM = "SENDER@EXAMPLE.COM"; // Replace with your "From" address. This address must be verified. const String TO = "RECIPIENT@EXAMPLE.COM"; // Replace with a "To" address. If your account is still in the // sandbox, this address must be verified. const String SUBJECT = "Amazon SES test (SMTP interface accessed using C#)"; const String BODY = "This email was sent through the Amazon SES SMTP interface by using C#."; // Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials. const String SMTP_USERNAME = "YOUR_SMTP_USERNAME"; // Replace with your SMTP username. const String SMTP_PASSWORD = "YOUR_SMTP_PASSWORD"; // Replace with your 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 an email through the Amazon SES SMTP interface..."); 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), you need to change HOST in Program.cs 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 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 will find the message that you sent.