Search in sources :

Example 11 with MimeMessagePreparator

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

the class LoggingMailSender method send.

@Override
public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException {
    for (MimeMessagePreparator preparator : mimeMessagePreparators) {
        try {
            MimeMessage mimeMessage = createMimeMessage();
            preparator.prepare(mimeMessage);
            LOG.info("\"Sending\" email: ");
            if (mimeMessage.getContent() instanceof MimeMultipart) {
                MimeMultipart msg = (MimeMultipart) mimeMessage.getContent();
                DataHandler dh = msg.getBodyPart(0).getDataHandler();
                ByteArrayOutputStream baos = null;
                try {
                    baos = new ByteArrayOutputStream();
                    dh.writeTo(baos);
                } catch (Exception e) {
                // Do nothing
                } finally {
                    try {
                        baos.close();
                    } catch (Exception e) {
                        LOG.error("Couldn't close byte array output stream");
                    }
                }
            } else {
                LOG.info(mimeMessage.getContent());
            }
        } catch (Exception e) {
            LOG.error("Could not create message", e);
        }
    }
}
Also used : MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MailException(org.springframework.mail.MailException)

Example 12 with MimeMessagePreparator

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

the class BulkEmailSenderService method sendEmail.

@Scheduled(fixedDelay = 120000)
private void sendEmail() {
    ArrayList<MimeMessagePreparator> mimeMessages = new ArrayList<MimeMessagePreparator>();
    logger.info("Executing now..." + mimeMessages.size() + " " + Thread.currentThread().getName() + " " + DEQUE.size());
    MimeMessagePreparator mimeMessage = DEQUE.pollFirst();
    if (mimeMessage == null) {
        logger.info("Nothing left to do getting out...");
        return;
    }
    while (mimeMessage != null) {
        mimeMessages.add(mimeMessage);
        logger.info("In while loop");
        if (mimeMessages.size() == 25) {
            logger.info("I've reached my limit...");
            break;
        } else {
            logger.info("Else");
            mimeMessage = DEQUE.pollFirst();
        }
    }
    logger.info("Sending Boom email..." + mimeMessages.size());
    mailSender.send(mimeMessages.toArray(new MimeMessagePreparator[mimeMessages.size()]));
    logger.info("I think i sent my mail..." + mimeMessages.size());
    sendEmail();
}
Also used : MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) ArrayList(java.util.ArrayList) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 13 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) {
    if (this.mailSender == null) {
        LOG.warn("Not sending email.  No mailer provided.");
        return;
    }
    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) 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) InputStreamReader(java.io.InputStreamReader) IOUtilities(org.finos.waltz.common.IOUtilities) 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) Checks(org.finos.waltz.common.Checks) Template(freemarker.template.Template) StringUtilities(org.finos.waltz.common.StringUtilities) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) Configuration(freemarker.template.Configuration) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) StringUtilities(org.finos.waltz.common.StringUtilities) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) HashMap(java.util.HashMap) Map(java.util.Map) Template(freemarker.template.Template)

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