This post discusses how Android apps 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:
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 e-mail solution or licensing, installing, and operating a third-party e-mail service.
This post shows a sample for the Android platform. The complete sample code and project files are included in the AWS SDK for Android. A link to the SDK is available at the end of this post.
To use 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 Android apps can record feedback from their users through Amazon SES. It requires that you already have a verified e-mail 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 e-mail 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 e-mails not shown here.
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.
AWSCredentials credentials = new BasicAWSCredentials( PropertyLoader.getInstance().getAccessKey(), PropertyLoader.getInstance().getSecretKey() );AmazonSimpleEmailServiceClient sesClient = new AmazonSimpleEmailServiceClient( credentials );
SES will accept both regular and raw e-mails. Our application makes use of the regular method, meaning we do not have to construct our own headers. Regular e-mails 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.
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 e-mail 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.
SendEmailRequest request = new SendEmailRequest(email,destination,feedbackMessage);SendEmailResult result = clientManager.ses().sendEmail(request);
A sample application that includes this code is provided in the AWS SDK for Android. The download link can be found on the following pages:
For more information about using AWS credentials with mobile applications see the following article:
Please feel free to ask questions or provide comments in the Mobile Development Forum.