Version 2 of the AWS Mobile SDK
- This article and sample apply to Version 1 of the AWS Mobile SDK. If you are building new apps, we recommend you use Version 2. For details, please visit the AWS Mobile SDK page.
- This content is being maintained for historical reference.
Sending E-mail with Amazon SES Using the AWS SDK for iOS and the AWS SDK for Android
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
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.
AmazonSESClient sesClient = [[AmazonSESClient alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
AWSCredentials credentials = new BasicAWSCredentials( PropertyLoader.getInstance().getAccessKey(), PropertyLoader.getInstance().getSecretKey() ); AmazonSimpleEmailServiceClient sesClient = new AmazonSimpleEmailServiceClient( credentials );
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.
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];
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);
Once we've constructed our email components, it simply
becomes a matter of creating a
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.
SESSendEmailRequest *ser = [[[SESSendEmailRequest alloc] init] autorelease]; ser.source = VERIFIED_EMAIL; ser.destination = destination; ser.message = message; SESSendEmailResponse response = [[AmazonClientManager ses] sendEmail:ser];
SendEmailRequest request = new SendEmailRequest(email,destination,feedbackMessage); SendEmailResult result = clientManager.ses().sendEmail(request);
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.