Send an email using SMTP with C# - Amazon Simple Email Service

This guide reflects the Classic Console (V1) for Amazon SES. For information about the New Console (V2) for Amazon SES, see the new Amazon Simple Email Service Developer Guide.

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

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.


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

      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 with your "From" address. // This address must be verified with Amazon SES. String FROM = ""; String FROMNAME = "Sender Name"; // Replace with a "To" address. If your account // is still in the sandbox, this address must be verified. String TO = ""; // Replace smtp_username with your Amazon SES SMTP user name. String SMTP_USERNAME = "smtp_username"; // Replace smtp_password with your Amazon SES SMTP password. 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. String CONFIGSET = "ConfigSet"; // If you're using Amazon SES in a region other than US West (Oregon), // replace with the Amazon SES SMTP // endpoint in the appropriate AWS Region. String HOST = ""; // 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. int PORT = 587; // The subject line of the email String SUBJECT = "Amazon SES test (SMTP interface accessed using C#)"; // The body of the email String BODY = "<h1>Amazon SES Test</h1>" + "<p>This email was sent through the " + "<a href=''>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); using (var client = new System.Net.Mail.SmtpClient(HOST, PORT)) { // Pass SMTP credentials client.Credentials = new NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD); // Enable SSL encryption client.EnableSsl = true; // Try to send the message. Show status in console. 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); } } } } }
  3. In Program.cs, replace the following email addresses with your own values:


    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:


    Your SMTP credentials are different from your AWS credentials. For more information about credentials, see Types of Amazon SES credentials.

    • 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 SMTP endpoint URLs for the AWS Regions where Amazon SES is available, see Amazon Simple Email Service (Amazon SES) in the AWS General Reference.

  6. (Optional) If you want to use a configuration set when sending this email, change the value of the variable CONFIGSET to the name of the configuration set. For more information about configuration sets, see Uso de conjuntos de configuración de Amazon SES.

  7. Save Program.cs.

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

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

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

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