Menu
Amazon Simple Email Service
Developer Guide

Send an Email Using SMTP with 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 Email Sending in Amazon SES.

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:

    using System; using System.Net; using System.Net.Mail; namespace AmazonSESSample { class Program { static void Main(string[] args) { // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. const String FROM = "sender@example.com"; const String FROMNAME = "Sender Name"; // Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified. const String TO = "recipient@example.com"; // Replace smtp_username with your Amazon SES SMTP user name. const String SMTP_USERNAME = "smtp_username"; // Replace smtp_password with your Amazon SES SMTP user name. const String SMTP_PASSWORD = "smtp_password"; // (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 below. const String CONFIGSET = "ConfigSet"; // 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. 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; // The subject line of the email const String SUBJECT = "Amazon SES test (SMTP interface accessed using C#)"; // The body of the email const String BODY = "<h1>Amazon SES Test</h1>" + "<p>This email was sent through the "+ "<a href='https://aws.amazon.com/ses'>Amazon SES</a> SMTP interface " + "using the .NET System.Net.Mail library.</p>"; // Create and build a new MailMessage object MailMessage message = new MailMessage(); message.IsBodyHtml = true; message.From = new MailAddress(FROM,FROMNAME); message.To.Add(new MailAddress(TO)); message.Subject = SUBJECT; message.Body = BODY; // Comment or delete the next line if you are not using a configuration set message.Headers.Add("X-SES-CONFIGURATION-SET", CONFIGSET); // Create and configure a new SmtpClient SmtpClient client = new SmtpClient(HOST, PORT); // Pass SMTP credentials client.Credentials = new NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD); // Enable SSL encryption client.EnableSsl = true; // Send the email. try { Console.WriteLine("Attempting to send email..."); client.Send(message); Console.WriteLine("Email sent!"); } catch (Exception ex) { Console.WriteLine("The email was not sent."); Console.WriteLine("Error message: " + ex.Message); } // Wait for a key press so that you can see the console output 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.

    • SENDER@EXAMPLE.COM—Replace with your "From" email address. You must verify this address before you run this program. For more information, see Verifying Identities 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.

  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.