com.nfbsoftware.sansserverplugin.sdk.aws.AmazonSESManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sans-server-plugin Show documentation
Show all versions of sans-server-plugin Show documentation
The NFB Software SansServer-Plugin serves two purposes, one as a development SDK and the other as a Maven plugin to build, provision, and deploy SansServer-based applications.
package com.nfbsoftware.sansserverplugin.sdk.aws;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.amazonaws.auth.AWSCredentials;
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;
import com.nfbsoftware.sansserverplugin.sdk.util.Entity;
import com.nfbsoftware.sansserverplugin.sdk.util.StringUtil;
/**
* The AmazonSESManager...
*
* @author Brendan Clemenzi
*/
public class AmazonSESManager
{
private Log m_logger;
private AmazonSimpleEmailServiceClient m_amazonSimpleEmailServiceClient;
private Properties m_properties;
/**
*
* @param properties
*/
public AmazonSESManager(Properties properties)
{
m_logger = LogFactory.getLog(this.getClass());
m_properties = properties;
String regionName = StringUtil.emptyIfNull(m_properties.getProperty(Entity.FrameworkProperties.AWS_REGION));
String accessKey = StringUtil.emptyIfNull(m_properties.getProperty(Entity.FrameworkProperties.AWS_ACCESS_KEY));
String secretKey = StringUtil.emptyIfNull(m_properties.getProperty(Entity.FrameworkProperties.AWS_SECRET_KEY));
// Init our aws credentials
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
m_amazonSimpleEmailServiceClient = new AmazonSimpleEmailServiceClient(credentials);
// Set our region
m_amazonSimpleEmailServiceClient.setRegion(Region.getRegion(Regions.fromName(regionName)));
}
/**
*
* @param toEmail
* @param messageSubject
* @param messageBody
* @throws Exception
*/
public void buildEmailMessage(String toEmail, String messageSubject, String messageBody) throws Exception
{
List toAddresses = new ArrayList();
toAddresses.add(toEmail);
buildEmailMessage(toAddresses, messageSubject, messageBody);
}
/**
*
* @param toAddresses
* @param messageSubject
* @param messageBody
* @throws Exception
*/
public void buildEmailMessage(List toAddresses, String messageSubject, String messageBody) throws Exception
{
String replyEmailAddress = StringUtil.emptyIfNull(m_properties.getProperty(Entity.FrameworkProperties.AWS_SES_REPLY_EMAIL_ADDRESS));
SendEmailRequest emailRequest = new SendEmailRequest();
emailRequest.withSource(replyEmailAddress);
Destination dest = new Destination().withToAddresses(toAddresses);
emailRequest.setDestination(dest);
Content subjContent = new Content().withData(messageSubject);
Message msg = new Message().withSubject(subjContent);
// Include a body in both text and HTML formats
Content htmlContent = new Content().withData(messageBody);
Body body = new Body().withHtml(htmlContent);
msg.setBody(body);
emailRequest.setMessage(msg);
m_amazonSimpleEmailServiceClient.sendEmail(emailRequest);
}
}