Thursday, February 10, 2011

Amazon Simple Email Service (SES) C# Code Example

Amazons Cloud guys have been busy recently. Their new service Simple Email Service (or SES) has gone into beta and seems like a winner for people who spend a lot of money on mail-shot services and would like to take some control. Assuming you have already signed up for AWS and generated a new access key pair, you should download the new AWS .Net SDK here.

You need to sign up to the service by optionally entering a VAT number associated with your business. Next you need to enter your telephone number and submit, almost immediately you will receive an automated phone call that lets you enter a 4-digit verification code that is displayed one you actually submit your phone number. All fairly painless and quick.

As the service is in Beta you cannot just email anyone using the service right now. You must verify each "from" email and each "to", "cc", or "bcc" email address. But the AWS SES FAQ located here: http://aws.amazon.com/ses/faqs/ seems to indicate that this will change.


1. Email verification

using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;

private void VerifyEmail(string EmailAddress)
{
//Set up the client with your access credentials
var client = new AmazonSimpleEmailServiceClient("AWSAccessKey", "AWSSecretKey");
var req = new VerifyEmailAddressRequest();
req.EmailAddress=EmailAddress;
var resp = client.VerifyEmailAddress(req);

//Note: the VerifyEmailAddress cannot tell you if the email has
//been verified yet as the verification involves an email being
//sent to that address and the user clicking on a long random
//looking Amazon link. That is when the actual verification
//happens. So nothing to do but wait.

//What is useful is the RequestId. This should be used in future
//when querying Amazon in relation to the request.
//resp.ResponseMetadata.RequestId

}

2. Actually Sending Email

using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;

private void SendEmail(string FromEmail, List ToEmailAddresses, string Subject, string EmailBody)
{
//Set up the client with your access credentials
var client = new AmazonSimpleEmailServiceClient("AWSAccessKey", "AWSSecretKey");
SendEmailRequest req = new SendEmailRequest()
    .WithDestination(new Destination() { BccAddresses = ToEmailAddresses })
    .WithSource(FromEmail)
    .WithReturnPath(FromEmail)
    .WithMessage(
    new Amazon.SimpleEmail.Model.Message(new Content(Subject),
    new Body().WithText(new Content(EmailBody))));

var resp = client.SendEmail(req);


//I fully qualified Message to disambiguate from
//System.Windows.Forms.Message if used in a WinForms app
}

3. Get Your Sending Quota
While in Beta this is set very low, for me it was max 200 email/day, and a rate of 1/second. But the FAQ located at: http://aws.amazon.com/ses/faqs/#28 says this will be steadily ramped up.

private void ShowMySendingQuota()
{
//Set up the client with your access credentials
var client = new AmazonSimpleEmailServiceClient("AWSAccessKey", "AWSSecretKey");
var resp = client.GetSendQuota(new GetSendQuotaRequest());

textBox1.Text = String.Format("Max24HourSend={0}, MaxSendRate={1}, SentLast24Hours={2}", resp.GetSendQuotaResult.Max24HourSend, resp.GetSendQuotaResult.MaxSendRate, resp.GetSendQuotaResult.SentLast24Hours);
}

4 comments:

  1. Can you port sample code for Raw Email.

    ReplyDelete
  2. Awesome! one tip I found is if you want to set the "from" display name, your FromEmail could be something like FromEmail = "\"John Smith\" <test@example.com>"

    ReplyDelete