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();
}
Aggregations