Sending E-mail with Amazon SES Using the AWS SDK for iOS and the AWS SDK for Android

Articles & Tutorials>SDKs>Sending E mail with Amazon SES Using the AWS SDK for iOS and the AWS SDK for Android
Sending E-mail with Amazon SES Using the AWS SDK for iOS and the AWS SDK for Android

Details

Submitted By: BobK@AWS
AWS Products Used: AWS SDK for Android, AWS SDK for iOS, Amazon SES
Created On: March 21, 2012 10:57 PM GMT
Last Updated: October 22, 2013 3:53 PM GMT

Sending E-mail with Amazon SES Using the AWS SDK for iOS and the AWS SDK for Android

Overview

This article discusses how mobile applications can use Amazon Web Services (AWS) to send e-mail without additional infrastructure. The sample code presented here uses Amazon Simple Email Service to record feedback from users but this same method could be used in the following scenarios:

  • automated alerts/bug reports
  • feature requests
  • blog by e-mail
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.

This article shows sample code for both the iOS and Android platforms. The complete sample code and project files are included in the AWS SDKs for these mobile platforms. Links to the SDKs are available at the end of this article.

To use the AWS SDK for iOS or the AWS SDK for Android, you will need AWS credentials, that is, an Access Key ID and Secret Access Key. If you haven't already signed up for Amazon Web Services, you will need to do that first to get your credentials. You can sign up for AWS here. After you sign up, you can retrieve your credentials at this page.

The sample application described here demonstrates how mobile applications can record feedback from their users through Amazon SES. It requires that you already have a verified email address; this address will be used as both the sender and recipient of the message, so it is not necessary to get production access to Amazon SES before using this sample application. You can verify an email address on the AWS console and read more about verification and production access in the Amazon SES Getting Started Guide. Amazon SES can also be used to create other types of emails not shown here.

Creating an Amazon SES Client

Making requests to Amazon SES requires creating a client for the service. The code below shows how to create a client on both the iOS and Android platforms.

  • iOS

AmazonSESClient sesClient = [[AmazonSESClient alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];            

  • Android

AWSCredentials credentials = new BasicAWSCredentials( PropertyLoader.getInstance().getAccessKey(), PropertyLoader.getInstance().getSecretKey() );
AmazonSimpleEmailServiceClient sesClient = new AmazonSimpleEmailServiceClient( credentials );

Creating Email

SES will accept both regular and raw emails. Our application makes use of the regular method, meaning we do not have to construct our own headers. Regular emails require a source, destination (list of to, cc, and bcc addresses) and a message, which itself comprises a body and subject. The code below shows how to create the various parts of the email on both the iOS and Android platforms.

  • iOS

SESContent *messageBody = [[[SESContent alloc] init] autorelease];
messageBody.data = [NSString stringWithFormat: @"Rating: %d\nComments:\n%@", rating.selectedSegmentIndex+1, commentsField.text];
        
SESContent *subject = [[[SESContent alloc] init] autorelease];
subject.data = [NSString stringWithFormat: @"Feedback from %@", nameField.text];
        
SESBody *body = [[[SESBody alloc] init] autorelease];
body.text = messageBody;
        
SESMessage *message = [[[SESMessage alloc] init] autorelease];
message.subject = subject;
message.body    = body;
        
SESDestination *destination = [[[SESDestination alloc] init] autorelease];
[destination.toAddresses addObject:VERIFIED_EMAIL];        

  • Android

String subjectText = "Feedback from " + nameField.getText();
Content subjectContent = new Content(subjectText);
    	
String bodyText = "Rating: " + ratingBar.getRating() + "\nComments\n" + commentsField.getText();
Body messageBody = new Body(new Content(bodyText));	
    	
Message feedbackMessage = new Message(subjectContent,messageBody);
    	
String email = PropertyLoader.getInstance().getVerifiedEmail();
Destination destination = new Destination().withToAddresses(email);

Sending Email

Once we've constructed our email components, it simply becomes a matter of creating a SendEmailRequest and passing this to the SES client we created earlier. The code below shows how to create a SendEmailRequest and send it with Amazon SES on both the iOS and Android platforms.

  • iOS

SESSendEmailRequest *ser = [[[SESSendEmailRequest alloc] init] autorelease];
ser.source      = VERIFIED_EMAIL;
ser.destination = destination;
ser.message     = message;
        
SESSendEmailResponse response = [[AmazonClientManager ses] sendEmail:ser];           

  • Android

SendEmailRequest request = new SendEmailRequest(email,destination,feedbackMessage);
SendEmailResult result = clientManager.ses().sendEmail(request);

References

A sample app that includes this code is hosted in our samples repositories on GitHub:

For more information about using AWS credentials with mobile applications see the following article:

Want to learn more?

To learn about mobile development best practices, follow our AWS Mobile Development Blog. You can also ask questions or post comments in the Mobile Development Forum about this or any other topic related to mobile development with AWS.

©2014, Amazon Web Services, Inc. or its affiliates. All rights reserved.