Search in sources :

Example 6 with MimeMessagePreparator

use of org.springframework.mail.javamail.MimeMessagePreparator in project OpenClinica by OpenClinica.

the class NotificationActionProcessor method createMimeMessagePreparator.

private void createMimeMessagePreparator(final ParticipantDTO pDTO, final String email) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {

        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setFrom(EmailEngine.getAdminEmail());
            message.setTo(email);
            message.setSubject(pDTO.getEmailSubject());
            message.setText(pDTO.getMessage());
        }
    };
    BulkEmailSenderService.addMimeMessage(preparator);
}
Also used : MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 7 with MimeMessagePreparator

use of org.springframework.mail.javamail.MimeMessagePreparator in project Settler by EmhyrVarEmreis.

the class EmailService method sendEmail.

@Async
public void sendEmail(List<String> to, EmailTemplate emailTemplate, Map<String, String> options) {
    EmailTemplateTmp emailTemplateTmp = EmailTemplateTmp.from(emailTemplate);
    for (Map.Entry<String, String> optionsEntry : options.entrySet()) {
        emailTemplateTmp.subject = emailTemplateTmp.subject.replaceAll("\\$" + optionsEntry.getKey(), optionsEntry.getValue());
        emailTemplateTmp.content = emailTemplateTmp.content.replaceAll("\\$" + optionsEntry.getKey(), optionsEntry.getValue());
    }
    MimeMessagePreparator preparator = mimeMessage -> {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
        message.setTo(to.toArray(new String[to.size()]));
        message.setFrom(settlerProperties.getMail().getFromAddress());
        message.setSubject(emailTemplateTmp.subject);
        message.setText(emailTemplateTmp.content, true);
    };
    try {
        mailSender.send(preparator);
    } catch (Exception e) {
        log.error("Error occurred during sendEmail", e);
    }
}
Also used : Async(org.springframework.scheduling.annotation.Async) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) EmailTemplateRepository(pl.morecraft.dev.settler.dao.repository.EmailTemplateRepository) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) HashMap(java.util.HashMap) List(java.util.List) Language(pl.morecraft.dev.settler.domain.dictionaries.Language) Stream(java.util.stream.Stream) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) Transaction(pl.morecraft.dev.settler.domain.Transaction) Service(org.springframework.stereotype.Service) EmailTemplate(pl.morecraft.dev.settler.domain.EmailTemplate) Map(java.util.Map) EmailTemplateType(pl.morecraft.dev.settler.domain.dictionaries.EmailTemplateType) Collections(java.util.Collections) SettlerProperties(pl.morecraft.dev.settler.configuration.SettlerProperties) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) HashMap(java.util.HashMap) Map(java.util.Map) Async(org.springframework.scheduling.annotation.Async)

Example 8 with MimeMessagePreparator

use of org.springframework.mail.javamail.MimeMessagePreparator 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)

Example 9 with MimeMessagePreparator

use of org.springframework.mail.javamail.MimeMessagePreparator in project books by aidanwhiteley.

the class MailClient method sendEmailsToAdminsForNewUsers.

public boolean sendEmailsToAdminsForNewUsers(List<User> newUsers) {
    boolean emailSent = true;
    MimeMessagePreparator messagePreparator = mimeMessage -> {
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        messageHelper.setFrom(registrationAdminEmailFrom);
        messageHelper.setTo(registrationAdminEmailTo);
        messageHelper.setSubject(registrationAdminEmailTitle);
        messageHelper.setText(prepareAdminNewUsersNotificationEmailContent(newUsers));
    };
    try {
        mailSender.send(messagePreparator);
    } catch (MailException me) {
        emailSent = false;
        LOGGER.error("Failed to send user registration emails for {}", newUsers, me);
    }
    return emailSent;
}
Also used : Value(org.springframework.beans.factory.annotation.Value) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) List(java.util.List) Logger(org.slf4j.Logger) User(com.aidanwhiteley.books.domain.User) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) Service(org.springframework.stereotype.Service) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) MailException(org.springframework.mail.MailException) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) MailException(org.springframework.mail.MailException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 10 with MimeMessagePreparator

use of org.springframework.mail.javamail.MimeMessagePreparator in project ma-core-public by infiniteautomation.

the class EmailSender method createPreparator.

public MimeMessagePreparator createPreparator(final InternetAddress from, final InternetAddress replyTo, final InternetAddress[] to, final InternetAddress[] cc, final InternetAddress[] bcc, final String subject, final EmailContent content) throws MailException {
    return new MimeMessagePreparator() {

        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, content.isMultipart(), content.getEncoding());
            helper.setFrom(from);
            if (replyTo != null)
                helper.setReplyTo(replyTo);
            helper.setTo(to);
            if (cc != null)
                helper.setCc(cc);
            if (bcc != null)
                helper.setBcc(bcc);
            // Ensure that line breaks in the subject are removed.
            String sub;
            if (subject == null)
                sub = "";
            else {
                sub = subject.replaceAll("\\r", "");
                sub = sub.replaceAll("\\n", "");
            }
            helper.setSubject(sub);
            if (content.getHtmlContent() == null)
                helper.setText(content.getPlainContent(), false);
            else if (content.getPlainContent() == null)
                helper.setText(content.getHtmlContent(), true);
            else
                helper.setText(content.getPlainContent(), content.getHtmlContent());
            for (EmailAttachment att : content.getAttachments()) att.attach(helper);
            for (EmailInline inline : content.getInlines()) inline.attach(helper);
        }
    };
}
Also used : MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Aggregations

MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)13 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)10 MimeMessage (javax.mail.internet.MimeMessage)7 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)5 Autowired (org.springframework.beans.factory.annotation.Autowired)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Value (org.springframework.beans.factory.annotation.Value)3 MailException (org.springframework.mail.MailException)3 Service (org.springframework.stereotype.Service)3 Configuration (freemarker.template.Configuration)2 Template (freemarker.template.Template)2 InputStreamReader (java.io.InputStreamReader)2 MessagingException (javax.mail.MessagingException)2 FreeMarkerTemplateUtils (org.springframework.ui.freemarker.FreeMarkerTemplateUtils)2 Event (alfio.model.Event)1 Configuration (alfio.model.system.Configuration)1