use of org.craftercms.commons.mail.EmailPreparationException 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();
}
use of org.craftercms.commons.mail.EmailPreparationException in project commons by craftercms.
the class EmailFactoryImpl method processTemplate.
protected String processTemplate(String templateName, Object templateModel) throws EmailException {
if (freeMarkerConfig == null) {
throw new EmailException(ERROR_KEY_TEMPLATE_CONFIG_MISSING);
}
templateName = templatePrefix + templateName + templateSuffix;
logger.debug(LOG_KEY_PROCESSING_EMAIL_TEMPLATE, templateName);
try {
Template template = freeMarkerConfig.getTemplate(templateName, templateEncoding);
StringWriter out = new StringWriter();
template.process(templateModel, out);
return out.toString();
} catch (IOException | TemplateException e) {
throw new EmailPreparationException(e);
}
}
Aggregations