Search in sources :

Example 1 with EmailPreparationException

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

Example 2 with EmailPreparationException

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);
    }
}
Also used : EmailPreparationException(org.craftercms.commons.mail.EmailPreparationException) StringWriter(java.io.StringWriter) TemplateException(freemarker.template.TemplateException) EmailException(org.craftercms.commons.mail.EmailException) IOException(java.io.IOException) Template(freemarker.template.Template)

Aggregations

EmailPreparationException (org.craftercms.commons.mail.EmailPreparationException)2 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 File (java.io.File)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 MessagingException (javax.mail.MessagingException)1 AddressException (javax.mail.internet.AddressException)1 EmailAddressException (org.craftercms.commons.mail.EmailAddressException)1 EmailException (org.craftercms.commons.mail.EmailException)1 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)1