Email with AWS SES
Given below is a useful solution to achieve quick delivery of emails using amazon SES (Simple Email Service).What is amazon SES:-
Simple Email Service offered under AWS is cloud based premium email service. All the resources, technology, smtp etc are hosted by aws. APIs are provided under AWS-SDK to use SES service. This service is totally managed by amazon and cloud based so no extra costing on your side. It also gives error report on bounced emails. This service is available to aws customers on pay per use basis and if you are under Free Service Tier then 62,000 messages per month to any recipient are free.
You can check more pricing details on aws website using http://aws.amazon.com/ses/pricing/
Pros:-
1. SES Email delivery is fast. It is good for auto generated emails, transactional emails, reports etc.
Cons:-
1. You need to verify sender email address before using that emailId in "From".
2. In Normal Access your reciver also need to be verified emailId under SES. It is not ease to verify your reciever before sending the acual email.
3. To address the above issue and to send email in bulk you need to subscribe to Production Access. Then you don't need to verify your recievers(EmailIds) under SES.
I am giving code samples to integrate and use SES in Java
Few steps that you need to follow are:-
Step 1: Get your aws account ready, verify your sender emailId and get your AWS ACCESS_KEY and SECRET_KEY ready.
Step 2: Now add AWS-SDK into your project. If you are using maven build then add dependency to your pom.xml
<!-- For AWS -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.9.30</version>
</dependency>
Otherwise you can also download library jar and include it in your project.
Step 3: Create a Utility Class to use SES Service
package com.aws.ses.sample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;
import com.amazonaws.services.simpleemail.model.Body;
import com.amazonaws.services.simpleemail.model.Content;
import com.amazonaws.services.simpleemail.model.Destination;
import com.amazonaws.services.simpleemail.model.Message;
import com.amazonaws.services.simpleemail.model.SendEmailRequest;
public class AmazonSESUtil {
private static final Logger logger = LoggerFactory.getLogger(AmazonSESUtil.class);
public static String sesEmail(String FROM, String TO, String SUBJECT, String BODY)
{
// Construct an object to contain the recipient address.
Destination destination = new Destination().withToAddresses(new String[]{TO});
// Create the subject and body of the message.
Content subject = new Content().withData(SUBJECT);
Content textBody = new Content().withData(BODY);
Body body = new Body().withText(textBody);
// Create a message with the specified subject and body.
Message message = new Message().withSubject(subject).withBody(body);
// Assemble the email.
SendEmailRequest request = new SendEmailRequest().withSource(FROM).withDestination(destination).withMessage(message);
try
{
logger.info("send an email through Amazon SES");
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(new BasicAWSCredentials("your access key", "your secret key"));
Region REGION = Region.getRegion(Regions.US_EAST_1);
client.setRegion(REGION);
// Send the email.
client.sendEmail(request);
System.out.println("Email sent!");
logger.info("Email sent!");
return "success";
}
catch (Exception ex)
{
logger.info("The email was not sent.");
logger.info("Error message: " + ex.getMessage());
return ex.getMessage();
}
}
}
Step 4: you can use the above utility as
AmazonSESUtil.sesEmail("your from email", "your to email", "Notification - Test Email", "This is test Email sent via aws SES Service");
Please write your queries and feedback.
0 Comments
Post a Comment