All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fivefaces.cloud.email.AWSSESEmailService Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fivefaces.cloud.email;

import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.model.*;
import com.fivefaces.cloud.Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

@Profile("EMAIL_AWS_SES")
@Slf4j
@Service
public class AWSSESEmailService implements EmailService {

    private final Utils utils;
    private AmazonSimpleEmailService sesClient;

    public AWSSESEmailService(Utils utils) {
        this.utils = utils;
    }

    @Override
    public void sendEmail(final String fromAddress, final String toAddress, final String subject, final String htmlBody, final String plainBody)  {
        doSendEmail(fromAddress, toAddress, subject, htmlBody, plainBody);
    }

    private void doSendEmail(final String fromAddress, final String toAddress, final String subject, final String htmlBody, final String plainBody) {
        SendEmailRequest request = new SendEmailRequest()
                .withDestination(
                        new Destination().withToAddresses(toAddress))
                .withMessage(new Message()
                        .withBody(new Body()
                                .withHtml(new Content().withCharset("UTF-8").withData(htmlBody)))
                        .withSubject(new Content().withCharset("UTF-8").withData(subject)))
                .withSource(fromAddress);

        if (StringUtils.isNotEmpty(plainBody)) {
            request.getMessage().getBody().withText(new Content().withCharset("UTF-8").withData(plainBody));
        }
        getSESClient().sendEmail(request);
        log.info("Sent Email to " + toAddress);
    }

    private AmazonSimpleEmailService getSESClient() {
        if (sesClient == null) {
            sesClient = utils.getAmazonSimpleEmailService();
        }
        return sesClient;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy