app/features/email/ses/SimpleEmailServiceImpl.java
/**
     * Creates a RawMessage using the JavaMail API and AWS Simple Email Service and allows
     * for a File attachment.
     *
     * @param from      Sender email address as a String
     * @param toEmails  Recipient email addresses as a String[]
     * @param ccEmails  CC recipient email addresses as a String[]
     * @param bccEmails BCC recipient email addresses as a String[]
     * @param subject   Email subject as a String
     * @param body      Email message body as a String
     * @param file      File attachment
     * @param asHtml    Message body as HTML or Plain Text
     * @throws EmailMessagingException
     */
Creates a RawMessage using the JavaMail API and AWS Simple Email Service and allows
for a File attachment.


Creates a RawMessage using the JavaMail API and AWS Simple Email Service and allows for a File attachment .
public void sendEmail(String from, String[] toEmails, String[] ccEmails, String[] bccEmails,
                          String subject, String body, File file, boolean asHtml)
            throws EmailMessagingException {
        if (toEmails == null || subject == null) {
            throw new EmailMessagingException(
                    new IllegalArgumentException(" You must provide a valid, non-null fromEmail, toEmail, and subject."));
        }
        Destination destination = new Destination().withToAddresses(toEmails);
        if (ccEmails != null) {
            destination.withCcAddresses(ccEmails);
        }
        if (bccEmails != null) {
            destination.withBccAddresses(bccEmails);
        }
        Session s = Session.getInstance(new Properties(), null);
        MimeMessage mimeMessage = new MimeMessage(s);
        try {
            mimeMessage.setSubject(subject);
        } catch (MessagingException e) {
            LOGGER.error("Could not set subject to mime message.", e);
            throw new EmailMessagingException("Could not set subject to mime message.", e);
        }
        MimeMultipart mimeMultipart = new MimeMultipart();
        BodyPart messagePart = new MimeBodyPart();
        try {
            if (asHtml) {
                messagePart.setContent(body, MediaType.TEXT_HTML);
            } else {
                messagePart.setContent(body, MediaType.TEXT_PLAIN);
            }
        } catch (MessagingException e) {
            LOGGER.error("Could not set body to mime message.", e);
            throw new EmailMessagingException("Could not set body to mime message.", e);
        }
        try {
            if (file != null) {
                MimeBodyPart filePart = new MimeBodyPart();
                filePart.attachFile(file);
                mimeMultipart.addBodyPart(filePart);
            }
        } catch (MessagingException | IOException e) {
            LOGGER.error("Could not set file attachment to mime body.", e);
            throw new EmailMessagingException("Could not set file attachment to mime body.", e);
        }
        try {
            mimeMessage.setContent(mimeMultipart);
        } catch (MessagingException e) {
            LOGGER.error("Mime multipart could not be set to the mime message.", e);
            throw new EmailMessagingException("Mime multipart could not be set to the mime message", e);
        }
        List<String> destinations = destination.getToAddresses();
        destinations.addAll(destination.getBccAddresses());
        destinations.addAll(destination.getCcAddresses());
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            mimeMessage.writeTo(outputStream);
            RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
            SendRawEmailRequest request = new SendRawEmailRequest()
                    .withSource(from)
                    .withDestinations(destinations)
                    .withRawMessage(rawMessage);
            sesClient.sendRawEmail(request);
        } catch (MessagingException | IOException e) {
            LOGGER.error("Could not write mime message to a byte array.", e);
        } catch (Exception e) {
            LOGGER.error("Could not send email via AWS Simple Email Service.", e);
            throw new EmailMessagingException("Could not send email via AWS Simple Email Service.", e);
        }
    }
public void sendEmail ( String from , String [ ] toEmails , String [ ] ccEmails , String [ ] bccEmails , String subject , String body , File file , boolean asHtml ) throws EmailMessagingException { if ( toEmails == null || subject == null ) { throw new EmailMessagingException ( new IllegalArgumentException ( " You must provide a valid, non-null fromEmail, toEmail, and subject." ) ) ; } Destination destination = new Destination ( ) . withToAddresses ( toEmails ) ; if ( ccEmails != null ) { destination . withCcAddresses ( ccEmails ) ; } if ( bccEmails != null ) { destination . withBccAddresses ( bccEmails ) ; } Session s = Session . getInstance ( new Properties ( ) , null ) ; MimeMessage mimeMessage = new MimeMessage ( s ) ; try { mimeMessage . setSubject ( subject ) ; } catch ( MessagingException e ) { LOGGER . error ( "Could not set subject to mime message." , e ) ; throw new EmailMessagingException ( "Could not set subject to mime message." , e ) ; } MimeMultipart mimeMultipart = new MimeMultipart ( ) ; BodyPart messagePart = new MimeBodyPart ( ) ; try { if ( asHtml ) { messagePart . setContent ( body , MediaType . TEXT_HTML ) ; } else { messagePart . setContent ( body , MediaType . TEXT_PLAIN ) ; } } catch ( MessagingException e ) { LOGGER . error ( "Could not set body to mime message." , e ) ; throw new EmailMessagingException ( "Could not set body to mime message." , e ) ; } try { if ( file != null ) { MimeBodyPart filePart = new MimeBodyPart ( ) ; filePart . attachFile ( file ) ; mimeMultipart . addBodyPart ( filePart ) ; } } catch ( MessagingException | IOException e ) { LOGGER . error ( "Could not set file attachment to mime body." , e ) ; throw new EmailMessagingException ( "Could not set file attachment to mime body." , e ) ; } try { mimeMessage . setContent ( mimeMultipart ) ; } catch ( MessagingException e ) { LOGGER . error ( "Mime multipart could not be set to the mime message." , e ) ; throw new EmailMessagingException ( "Mime multipart could not be set to the mime message" , e ) ; } List < String > destinations = destination . getToAddresses ( ) ; destinations . addAll ( destination . getBccAddresses ( ) ) ; destinations . addAll ( destination . getCcAddresses ( ) ) ; try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream ( ) ; mimeMessage . writeTo ( outputStream ) ; RawMessage rawMessage = new RawMessage ( ByteBuffer . wrap ( outputStream . toByteArray ( ) ) ) ; SendRawEmailRequest request = new SendRawEmailRequest ( ) . withSource ( from ) . withDestinations ( destinations ) . withRawMessage ( rawMessage ) ; sesClient . sendRawEmail ( request ) ; } catch ( MessagingException | IOException e ) { LOGGER . error ( "Could not write mime message to a byte array." , e ) ; } catch ( Exception e ) { LOGGER . error ( "Could not send email via AWS Simple Email Service." , e ) ; throw new EmailMessagingException ( "Could not send email via AWS Simple Email Service." , e ) ; } }
Creates a RawMessage using the JavaMail API and AWS Simple Email Service and allows
 for a File attachment.
Creates a RawMessage using the JavaMail API and AWS Simple Email Service and allows for a File attachment .
// JavaMail representation of the message // Add Message parts // Add File parts // Set initialized mimeMultipart to the mimeMessage // Build complete destination list // Create Raw message from MimeMessage // Create Raw Email Request // Send Email via the AmazonSimpleEmailServiceClient