Search in sources :

Example 1 with StringUtilities

use of com.khartec.waltz.common.StringUtilities in project waltz by khartec.

the class WaltzEmailer method sendEmail.

public void sendEmail(String subject, String body, String[] to) {
    Checks.checkNotEmpty(subject, "subject cannot be empty");
    Checks.checkNotEmpty(body, "body cannot be empty");
    Checks.checkNotEmpty(to, "to cannot be empty");
    Checks.checkAll(to, StringUtilities::notEmpty, "email address cannot be empty");
    MimeMessagePreparator preparator = mimeMessage -> {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
        message.setSubject(subject);
        message.setFrom(fromEmail);
        message.setBcc(to);
        message.addAttachment("waltz.png", IOUtilities.getFileResource("/images/waltz.png"));
        message.addAttachment("client-logo", IOUtilities.getFileResource("/templates/images/client-logo.png"));
        Map model = new HashMap();
        model.put("body", body);
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
        try (InputStreamReader templateReader = new InputStreamReader(IOUtilities.getFileResource(DEFAULT_EMAIL_TEMPLATE_LOCATION).getInputStream())) {
            Template template = new Template("template", templateReader, cfg);
            String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
            message.setText(text, true);
        }
    };
    this.mailSender.send(preparator);
}
Also used : MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Autowired(org.springframework.beans.factory.annotation.Autowired) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) HashMap(java.util.HashMap) IOUtilities(com.khartec.waltz.common.IOUtilities) InputStreamReader(java.io.InputStreamReader) Checks(com.khartec.waltz.common.Checks) Value(org.springframework.beans.factory.annotation.Value) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) Configuration(freemarker.template.Configuration) Service(org.springframework.stereotype.Service) Map(java.util.Map) FreeMarkerTemplateUtils(org.springframework.ui.freemarker.FreeMarkerTemplateUtils) Template(freemarker.template.Template) StringUtilities(com.khartec.waltz.common.StringUtilities) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) Configuration(freemarker.template.Configuration) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) StringUtilities(com.khartec.waltz.common.StringUtilities) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) HashMap(java.util.HashMap) Map(java.util.Map) Template(freemarker.template.Template)

Aggregations

Checks (com.khartec.waltz.common.Checks)1 IOUtilities (com.khartec.waltz.common.IOUtilities)1 StringUtilities (com.khartec.waltz.common.StringUtilities)1 Configuration (freemarker.template.Configuration)1 Template (freemarker.template.Template)1 InputStreamReader (java.io.InputStreamReader)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Value (org.springframework.beans.factory.annotation.Value)1 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)1 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)1 MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)1 Service (org.springframework.stereotype.Service)1 FreeMarkerTemplateUtils (org.springframework.ui.freemarker.FreeMarkerTemplateUtils)1