Using Amazon SES (and Possibly Other Services) on Google AppEngine

After mentioning my patch for AWS SDK on google-appengine mailing list I got few inquiries so I decided to write a post with more details about how to use it…

1. Get patched AWS SDK from https://github.com/radomirml/aws-sdk-for-gaej

This adapted version makes possible use of AWS SDK on Google AppEngine allowing user to provide own HTTP Client Factory implementation.  A custom HTTP Client Factory class is needed to provide an implementation of HTTPClient that sends requests using GAE’s URL Fetch service.

Disclaimer: Currently, I’m using the patched library with SES service only. It should work with other AWS SDK APIs as well but I didn’t try it. For SES only, you can download the jar file I built stripping out unneeded APIs.

Use the built jar file to replace the original aws-java-sdk jar file. AWS SDK depends on Apache HttpClient 4 so you’ll need to add its jars your project too.

2. Get (or make) GAE compatible ClientConnectionManager class

Get and use the ClientConnectionManager implementation created by ESXX or you may want to use it as a starting point and customize according to your needs.

3. Send email using SES

Assuming you were already using JavaMail API to send emails under GAE, you probably have a line like:

  Transport.send(msg);

that dispatches your message. Replace this line with a block of code that will send the email via SES:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Capture the raw message
ByteArrayOutputStream out = new ByteArrayOutputStream();
msg.writeTo(out);
String rawString = out.toString();

RawMessage rm = new RawMessage();
rm.setData(ByteBuffer.wrap(rawString.getBytes()));

ClientConfiguration cc = new ClientConfiguration();
cc.setHttpClientFactory(new HttpClientFactory() {
  public HttpClient createHttpClient(ClientConfiguration config) {
      return new DefaultHttpClient(new GAEConnectionManager(), new BasicHttpParams());
  }
});

// Set AWS access credentials
AmazonSimpleEmailServiceClient client =
  new AmazonSimpleEmailServiceClient(
      new BasicAWSCredentials(
              AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_KEY),
      cc);

// Call Amazon SES to send the message 
client.sendRawEmail(new SendRawEmailRequest().withRawMessage(rm)); 
  

Comments