Search in sources :

Example 1 with EmailAddressException

use of org.craftercms.commons.mail.EmailAddressException in project commons by craftercms.

the class EmailFactoryImpl method createMessage.

protected MimeMessage createMessage(String from, String[] to, String[] cc, String[] bcc, String replyTo, String subject, String body, boolean html, File... attachments) throws EmailException {
    boolean addAttachments = ArrayUtils.isNotEmpty(attachments);
    MimeMessageHelper messageHelper;
    try {
        if (addAttachments) {
            messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true);
        } else {
            messageHelper = new MimeMessageHelper(mailSender.createMimeMessage());
        }
        messageHelper.setFrom(from);
        if (to != null) {
            messageHelper.setTo(to);
        }
        if (cc != null) {
            messageHelper.setCc(cc);
        }
        if (bcc != null) {
            messageHelper.setBcc(bcc);
        }
        if (replyTo != null) {
            messageHelper.setReplyTo(replyTo);
        }
        messageHelper.setSubject(subject);
        messageHelper.setText(body, html);
        if (addAttachments) {
            for (File attachment : attachments) {
                messageHelper.addAttachment(attachment.getName(), attachment);
            }
        }
    } catch (AddressException e) {
        throw new EmailAddressException(e);
    } catch (MessagingException e) {
        throw new EmailPreparationException(e);
    }
    logger.debug(LOG_KEY_MIME_MSG_CREATED, from, StringUtils.join(to, ','), StringUtils.join(cc, ','), StringUtils.join(bcc, ','), subject, body);
    return messageHelper.getMimeMessage();
}
Also used : EmailPreparationException(org.craftercms.commons.mail.EmailPreparationException) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) EmailAddressException(org.craftercms.commons.mail.EmailAddressException) EmailAddressException(org.craftercms.commons.mail.EmailAddressException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) File(java.io.File)

Aggregations

File (java.io.File)1 MessagingException (javax.mail.MessagingException)1 AddressException (javax.mail.internet.AddressException)1 EmailAddressException (org.craftercms.commons.mail.EmailAddressException)1 EmailPreparationException (org.craftercms.commons.mail.EmailPreparationException)1 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)1