Menu
Amazon Simple Email Service
Developer Guide

Amazon SES .NET/C# Code Examples

This section contains the following sample code:

Sending Email Using the AWS SDK for .NET

The following sample code is a complete solution for sending email through Amazon SES using the AWS SDK for .NET. This sample code 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.

Copy
using Amazon; using System; using System.Collections.Generic; using System.Configuration; using Amazon.SimpleEmail; using Amazon.SimpleEmail.Model; namespace AmazonSESSDKSample { class Program { // Change the senderAddress and receiverAddress fields to values that // match your senderAddress and receiverAddress. // 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"; static void Main(string[] args) { if (CheckRequiredFields()) { using (var client = new AmazonSimpleEmailServiceClient()) { var sendRequest = new SendEmailRequest { Source = senderAddress, Destination = new Destination { ToAddresses = new List<string> { receiverAddress } }, Message = new Message { Subject = new Content("Amazon SES test (AWS SDK for .NET)"), Body = new Body { Text = new Content("This email " + "was sent through Amazon SES using the " + "AWS SDK for Java.") } } }; try { Console.WriteLine("Sending email using AWS 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(); } static bool CheckRequiredFields() { var appConfig = ConfigurationManager.AppSettings; if (string.IsNullOrEmpty(appConfig["AWSProfileName"])) { Console.WriteLine("AWSProfileName was not set in the App.config" + "file."); return false; } if (string.IsNullOrEmpty(senderAddress)) { Console.WriteLine("The variable senderAddress is not set."); return false; } if (string.IsNullOrEmpty(receiverAddress)) { Console.WriteLine("The variable receiverAddress is not set."); return false; } return true; } } }

Sending Email through the Amazon SES SMTP Interface Using C#

The following sample code is a complete solution for sending email through the Amazon SES SMTP interface using C#. In order to run this sample code, you must obtain SMTP credentials; for more information, see Obtaining Your Amazon SES SMTP Credentials.

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-east-1.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(); } } }