Tag: .NET


Getting Started with Amazon SES and .NET

by Rohan Deshpande | on | in How To | | Comments

This post will help get you started using Amazon SES from .NET applications. Before you start working, make sure you are signed up for Amazon SES as described in the Getting Started Guide. This post assumes you are using Visual Studio for development.

Using SMTP

The easiest way to send email through Amazon SES is via SMTP. The Amazon SES SMTP endpoint requires delivery over authenticated encrypted connections (Transport Layer Security, or TLS).

  1. Generate your SMTP credentials according to these instructions. Please note that your SMTP credentials are different from your AWS credentials.
  2. Create a Console Application in Visual Studio.
  3. Add the following code to the Main method:
      String username = "SMTP-USERNAME";  // Replace with your SMTP username.
      String password = "SMTP-PASSWORD";  // Replace with your SMTP password.
      String host = "email-smtp.us-east-1.amazonaws.com";
      int port = 25;
       
      using (var client = new System.Net.Mail.SmtpClient(host, port))
      {
        client.Credentials = new System.Net.NetworkCredential(username, password);
        client.EnableSsl = true;
       
        client.Send
        (
                  "FROM@EXAMPLE.COM",  // Replace with the sender address.
                  "TO@EXAMPLE.COM",    // Replace with the recipient address.
                  "Testing Amazon SES through SMTP",			  
                  "This email was delivered through Amazon SES via the SMTP end point."
        );
      }	
  4. Run the application and check the recipient mailbox for your email.
  5. Advanced users can configure the SMTP credentials and SMTP end point using the <mailSettings> element as described in this Amazon SES forum post.

Using the AWS SDK

The AWS SDK gives you more control over email delivery through Amazon SES. Messages are delivered over HTTPS to the Amazon SES API.

  1. Create a Console Application in Visual Studio.
  2. Install the AWS SDK for .NET.
  3. SendEmail is the simplest action provided by the Amazon SES SDK. To send an email using this action, add the following code to your Main method:
       String source = "FROM@EXAMPLE.COM";        // Replace with the sender address.
       String recipient = "TO@EXAMPLE.COM";       // Replace with the recipient address.
       String awsAccessKey = "AWS-ACCESS-KEY";    // Replace with your AWS access key.
       String awsSecretKey = "AWS-SECRET-KEY";    // Replace with your AWS secret key.
        
       // Setup the email recipients.
       var oDestination = new Destination().WithToAddresses(new List<string>() { recipient });
        
       // Create the email subject.
       var oSubject = new Content().WithData("Testing Amazon SES through the API");
        
       // Create the email body.
       var oTextBody = new Content().WithData("This email was delivered through the Amazon SES API.");
       var oBody = new Body().WithText(oTextBody);
        
       // Create and transmit the email to the recipients via Amazon SES.
       var oMessage = new Message().WithSubject(oSubject).WithBody(oBody);
       var request = new SendEmailRequest().WithSource(source).WithDestination(oDestination).WithMessage(oMessage);
        
       using (var client = new AmazonSimpleEmailServiceClient(awsAccessKey, awsSecretKey))
       {
       		 client.SendEmail(request);
       }	 
  4. Run the application and check the recipient mailbox for your email.

Final Thoughts

We hope that you now have the basic information on how to get started with Amazon SES using .NET. Please review our API reference; it describes all actions, error codes and restrictions that apply to Amazon SES.

If you have comments or feedback about this service, please post them on the Amazon SES forums. We actively monitor the forum and frequently engage with customers. Happy sending with Amazon SES!