Emailing using AWS


Configure AWS Keys as follows on your properties file or AWS variables,

AWS_ACCESS_KEY_ID =
AWS_SECRET_KEY =

Configure Email Parameters,

FROM_EMAIL = noreply@aaaa.com
SMTP_USER = AAAA
SMTP_PASSWORD = AAAA
SMTP_HOST = email-smtp.us-east-1.amazonaws.com
SMTP_PORT = 25

Configure servlet.xml as follows for the EmailUtil 

     <bean id="emailUtil" class="com.EmailUtil">
<constructor-arg value="${FROM_EMAIL}" />
<constructor-arg value="${SMTP_USER}" />
<constructor-arg value="${SMTP_PASSWORD}" />
<constructor-arg value="${SMTP_HOST}" />
<constructor-arg value="${SMTP_PORT}" />
<constructor-arg value="${AWS_ACCESS_KEY_ID}" />
<constructor-arg value="${AWS_SECRET_KEY}" />
</bean>

The EmailUtil can be as follows,

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;

@Component
public class EmailUtil {
String fromEmail;
String smtpUsername;
String smtpPassword;
String smtpHost;
String smtpPort;
String awsKey;
String awsSecreat;

public EmailUtil(String fromEmail,  String smtpUsername,  String smtpPassword, 
                                    String smtpHost,    String smtpPort,            String awsKey, 
                                    String awsSecreat) {
this.fromEmail = fromEmail;
this.smtpUsername = smtpUsername;
this.smtpPassword = smtpPassword;
this.smtpHost = smtpHost;
this.smtpPort = smtpPort;
this.awsKey = awsKey;
this.awsSecreat = awsSecreat;
}
public String sendEmail(String toEmail, String emailSubject,String emailBody) {
String messages = null;
Destination destination = new Destination().withToAddresses(new String[]{toEmail});
                Content subject = new Content().withData(emailSubject);
                Content textBody = new Content().withData(emailBody); 
                Body body = new Body().withHtml(textBody);
                Message message = new Message().withSubject(subject).withBody(body);
                SendEmailRequest request = new                          SendEmailRequest().withSource(fromEmail).withDestination(destination).withMessage(message);
                try
                {        
                     AWSCredentials awsCredentials = new BasicAWSCredentials(awsKey, awsSecreat);
                     AmazonSimpleEmailServiceClient client = 
                                          new  AmazonSimpleEmailServiceClient(awsCredentials);
                     Region REGION = Region.getRegion(Regions.US_EAST_1);
                    client.setRegion(REGION);
                    client.sendEmail(request);  
                }
                catch (Exception ex) 
               {
                 log.debug("The email was not sent.");
log.debug("Error message: ", ex);
messages = ex.getMessage();
                 }
               return messages; 
       }
}

Comments

Popular posts from this blog

API design best practices

DB Connection Issues

Reading Excel Sheet on client side and processing it