Tour de force

WooExpert Platinum WooCommerce partner - biggest elephant in the eCommerce room.
Mailchimp Partner Mailchimp partner - you know what’s the newest cutting edge customer experience solution? Great email subject!
Clutch logo Clutch - clients say we’re top dogs in eCommerce. Throw us that stick now.
WordPress Vip WordPress VIP - Enterprise hosting partner

Amazon SES – Simple Email Service – C# code examples [ASP.NET CODES]

Amazon Simple Email Service (Amazon SES) is a highly scalable and cost-effective bulk and transactional email sending service for businesses and developers. Amazon SES eliminates the complexity and expense of building an in-house email solution or licensing, installing, and operating a third-party email service for this type of email communication.

Listen to this article
1x
0.5x 0.75x 1x 1.25x 1.5x

In addition, the service integrates with other AWS services, making it easy to send emails from applications being hosted on AWS. With Amazon SES there is no long-term commitment, minimum spend or negotiation required – businesses can utilize a free usage tier and beyond that pay only low fees for the number of emails sent plus data transfer fees. You can find out more on official AWS SES page.

Here are some of the code snippets in C# (ASP.NET 4.0) but they will also work on earlier .NET versions. Please note that we developed these codes on top of AWS SDK for.NET! These libraries are important for code snippets to work so please implement them in your project before you start to copy & paste 🙂

First, we need to insert AKID and secret key into our Configuration file This step is important as it gives you one point of contact for your access credentials and it is also much safer

 

List Verified Emails Script (Verified emails are valid addresses from which you can send mass emails)

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;

public partial class ListVerifiedEmails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//INITIALIZE AWS CLIENT/////////////////////////////////////////////////////////
AmazonSimpleEmailServiceConfig amConfig = new AmazonSimpleEmailServiceConfig();
amConfig.UseSecureStringForAwsSecretKey = false;
AmazonSimpleEmailServiceClient amzClient = new AmazonSimpleEmailServiceClient(ConfigurationManager.AppSettings["AKID"].ToString(), ConfigurationManager.AppSettings["Secret"].ToString(), amConfig);

//LIST VERIFIED EMAILS/////////////////////////////////////////////////////////
ListVerifiedEmailAddressesRequest lveReq = new ListVerifiedEmailAddressesRequest();
ListVerifiedEmailAddressesResponse lveResp = amzClient.ListVerifiedEmailAddresses(lveReq);
ListVerifiedEmailAddressesResult lveResult = lveResp.ListVerifiedEmailAddressesResult;

foreach (Object email in lveResult.VerifiedEmailAddresses)
{
Response.Write(email.ToString()+"
");
}
}
}

Verify email (Verified emails are valid addresses from which you can send mass emails)

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;

public partial class VerifyEmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//INITIALIZE AWS CLIENT/////////////////////////////////////////////////////////
AmazonSimpleEmailServiceConfig amConfig = new AmazonSimpleEmailServiceConfig();
amConfig.UseSecureStringForAwsSecretKey = false;
AmazonSimpleEmailServiceClient amzClient = new AmazonSimpleEmailServiceClient(ConfigurationManager.AppSettings["AKID"].ToString(), ConfigurationManager.AppSettings["Secret"].ToString(), amConfig);

//VERIFY EMAIL/////////////////////////////////////////////////////////
VerifyEmailAddressRequest veaRequest = new VerifyEmailAddressRequest();
veaRequest.EmailAddress = "INSERT_EMAIL_TO_VERIFY_HERE";
VerifyEmailAddressResponse veaResponse = amzClient.VerifyEmailAddress(veaRequest);
Response.Write(veaResponse.ResponseMetadata.RequestId);
}
}

Get sending quota limits

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;

public partial class GetQuota : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//INITIALIZE AWS CLIENT/////////////////////////////////////////////////////////
AmazonSimpleEmailServiceConfig amConfig = new AmazonSimpleEmailServiceConfig();
amConfig.UseSecureStringForAwsSecretKey = false;
AmazonSimpleEmailServiceClient amzClient = new AmazonSimpleEmailServiceClient(ConfigurationManager.AppSettings["AKID"].ToString(), ConfigurationManager.AppSettings["Secret"].ToString(), amConfig);

//GET SENDING QUOTA/////////////////////////////////////////////////////////
GetSendQuotaRequest gsqReq = new GetSendQuotaRequest();
GetSendQuotaResponse gsqResp = amzClient.GetSendQuota(gsqReq);
GetSendQuotaResult gsqResult = gsqResp.GetSendQuotaResult;

Response.Write("Max 24 hour send limit: " + gsqResult.Max24HourSend.ToString() + ";
Max send rate: " + gsqResult.MaxSendRate.ToString() + ";
Send last 24 hours: " + gsqResult.SentLast24Hours.ToString());
}
}

Get sending statistics (Bounces, Complaints, Deliveries, Rejects…from last two weeks)

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;

public partial class GetSendStatistics : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//INITIALIZE AWS CLIENT/////////////////////////////////////////////////////////
AmazonSimpleEmailServiceConfig amConfig = new AmazonSimpleEmailServiceConfig();
amConfig.UseSecureStringForAwsSecretKey = false;
AmazonSimpleEmailServiceClient amzClient = new AmazonSimpleEmailServiceClient(ConfigurationManager.AppSettings["AKID"].ToString(), ConfigurationManager.AppSettings["Secret"].ToString(), amConfig);

//GET SEND STATISTICS/////////////////////////////////////////////////////////
GetSendStatisticsRequest gssReq = new GetSendStatisticsRequest();
GetSendStatisticsResponse gssResp = amzClient.GetSendStatistics(gssReq);
GetSendStatisticsResult gssResult = gssResp.GetSendStatisticsResult;

foreach (SendDataPoint sdp in gssResult.SendDataPoints)
{
Response.Write("BOUNCES:" + sdp.Bounces.ToString() + "; COMPLAINTS:" + sdp.Complaints.ToString() + "; DELIVERY ATTEMPTS:" + sdp.DeliveryAttempts.ToString() + "; REJECTS:" + sdp.Rejects.ToString() + "; DateTime:" + sdp.Timestamp.ToLongDateString() + " - " + sdp.Timestamp.ToLongTimeString() + "

");
}
}
}

Send email-to-many (You can only send emails from verified addresses). Please note that your sending quota applies here and that you can send to max. 50 emails in one method call. That means you must divide your email array / que / sending list in groups of 50 by 50. PLEASE STUDY QUOTA MANAGEMENT BEFORE SENDING MASS EMAILS

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;

public partial class EmailTestingSender : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//INITIALIZE AWS CLIENT/////////////////////////////////////////////////////////
AmazonSimpleEmailServiceConfig amConfig = new AmazonSimpleEmailServiceConfig();
amConfig.UseSecureStringForAwsSecretKey = false;
AmazonSimpleEmailServiceClient amzClient = new AmazonSimpleEmailServiceClient(ConfigurationManager.AppSettings["AKID"].ToString(), ConfigurationManager.AppSettings["Secret"].ToString(), amConfig);

ArrayList to = new ArrayList();

//ADD AS TO 50 emails per one sending method//////////////////////
to.Add("INSERT EMAIL 1 HERE");
to.Add("INSERT EMAIL 2 HERE");

//ADD AS TO 50 emails per one sending method/////////////////////
Destination dest = new Destination();
dest.WithBccAddresses((string[])to.ToArray(typeof(string)));
string body = "INSERT HTML BODY HERE";
string subject = "INSERT EMAIL SUBJECT HERE";
Body bdy = new Body();
bdy.Html = new Amazon.SimpleEmail.Model.Content(body);
Amazon.SimpleEmail.Model.Content title = new Amazon.SimpleEmail.Model.Content(subject);
Message message = new Message(title, bdy);
SendEmailRequest ser = new SendEmailRequest("VERIFIED_EMAIL_HERE", dest, message);
SendEmailResponse seResponse = amzClient.SendEmail(ser);
SendEmailResult seResult = seResponse.SendEmailResult;
}
}

What are your send methods and codes? Do you have some advice or experience from SES? Please tell us and share stories in the comments!

Happy coding 🙂

UPDATE: Amazon has extensively updated its Admin panel so you can now perform various checks on bounces, deliveries, complaints and rejects without API coding.  Also, you can setup SMTP access for SES and verify new senders. So head on to the official AWS management console and see all of the cool features for yourself!

Krešimir Končić
Krešimir Končić Owner at Neuralab

Ex QBASIC developer that ventured into a web world in 2007. Leading a team of like-minded Open Source aficionados that love design, code and a pinch of BBQ. Currently writing a book that explains why ‘coding is the easier part’ of our field.


Subscribe to our quarterly newsletter

Please fill in this field.
Please thick this field to proceed.

Related blog posts