Example usage for org.springframework.mail.javamail MimeMessagePreparator MimeMessagePreparator

List of usage examples for org.springframework.mail.javamail MimeMessagePreparator MimeMessagePreparator

Introduction

In this page you can find the example usage for org.springframework.mail.javamail MimeMessagePreparator MimeMessagePreparator.

Prototype

MimeMessagePreparator

Source Link

Usage

From source file:net.bafeimao.umbrella.web.service.UserService.java

private void sendRegistrationConfirmEmail(final User user) {
    final MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setTo(user.getEmail());
            message.setFrom("29283212@qq.com");
            message.setSubject("coconut?");

            Map model = new HashMap();
            model.put("user", user);
            String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
                    "/templates/registration-confirm-mail.html", "gb2312", model);
            message.setText(text, true);
        }//  ww  w . j  a  v  a  2s. com
    };
    this.mailSender.send(preparator);
}

From source file:no.dusken.common.control.MailController.java

/**
 *
 * @param mail the mail part of the mail
 * @param model other things that should be in the mail.
 * @param template - the path to the velocitytemplate to use (mail/template.vm)
 *//*from  ww  w .ja v  a2s .  c  o  m*/
public void sendEmail(final Mail mail, final Map model, final String template) {
    final Map<String, Object> map = new HashMap<String, Object>();
    if (model != null) {
        map.putAll(model);
    }
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setTo(mail.getToAddress());
            if (mail.getFromAddress() == null || mail.getFromAddress().equals("")) {
                mail.setFromAddress(defaultSenderAddress);
            }
            message.setFrom(mail.getFromAddress());
            message.setSubject(mail.getSubject());

            map.put("mail", mail);
            String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, map);
            message.setText(text, false);
            if (mail.getAttachment() != null) {
                FileSystemResource res = new FileSystemResource(mail.getAttachment());
                message.addAttachment(mail.getAttachment().getName(), res);
            }
        }
    };
    this.mailSender.send(preparator);
}

From source file:org.ktunaxa.referral.server.command.test.TestEmailCommand.java

@Override
public void execute(TestEmailRequest request, TestEmailResponse response) throws Exception {
    final String from = "rms@ktunaxa.org";
    final String to = request.getTo();
    if (null == to) {
        throw new GeomajasException(ExceptionCode.PARAMETER_MISSING, "to");
    }/* w ww .ja v  a2s  . c om*/
    MimeMessagePreparator preparator = new MimeMessagePreparator() {

        public void prepare(MimeMessage mimeMessage) throws Exception {

            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            mimeMessage.setFrom(new InternetAddress(from));
            mimeMessage.setSubject("Test");
            mimeMessage.setText("Testing RMS");
        }
    };
    mailSender.send(preparator);
}

From source file:org.gaixie.micrite.mail.impl.EmailSenderImpl.java

public void sendEmail(String from, List<String> recipients, String subj, String text) {

    final String fm = from;
    final String s = subj;
    final String tpl = text;

    for (final String to : recipients) {
        // ??????
        Runnable thread = new Runnable() {
            public void run() {
                try {
                    mailSender.send(new MimeMessagePreparator() {
                        public void prepare(MimeMessage mimeMessage) throws Exception {
                            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "UTF-8");
                            if (fm != null)
                                helper.setFrom(fm);
                            else
                                helper.setFrom("micrite-noreply@gmail.com");
                            if (s != null)
                                helper.setSubject(s);
                            else
                                helper.setSubject("no subject");
                            if (tpl != null)
                                helper.setText(tpl);
                            else
                                helper.setText("no text");
                            helper.setTo(to);
                        }/*from  www . j  a  v  a  2s  .  c  o  m*/
                    });
                } catch (MailException ex) {
                    // simply log it and go on...
                    System.err.println(ex.getMessage());
                }
            }
        };
        new Thread(thread).start();
    }
}

From source file:com.wisemapping.mail.Mailer.java

public void sendEmail(final String from, final String to, final String subject, final Map model,
        @NotNull final String templateMail) {
    final MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setTo(to);/*from  w ww . ja  va  2s .c  om*/
            message.setFrom(from);
            message.setSubject(subject);

            final String messageBody = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
                    "/mail/" + templateMail, model);
            message.setText(messageBody, true);
        }
    };

    this.mailSender.send(preparator);
}

From source file:ch.javaee.basicMvc.service.MailSenderService.java

@Async
public void sendAuthorizationMail(final User user, final SecurityCode securityCode) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setSubject(mailBean.getSubject());
            message.setTo(user.getEmail());
            message.setFrom(mailBean.getFrom());
            Map model = new HashMap();
            model.put("websiteName", mailBean.getWebsiteName());
            model.put("host", mailBean.getHost());
            model.put("user", user);
            model.put("securityCode", securityCode);
            String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, CONFIRMATION_TEMPLATE,
                    model);/*from  ww  w  .j a v  a2  s .  c om*/
            message.setText(text, true);
        }
    };
    this.mailSender.send(preparator);
}

From source file:com.miserablemind.butter.domain.service.email.EmailService.java

/**
 * Sends a mime mail with a specified Velocity template that may contain HTML and attachments.
 *
 * @param emailMessage prepared message object to be sent. Usually prepared by {@link EmailManager}
 *//*from   w  ww .  j  a v a 2  s.  c o  m*/
public void sendMimeMail(final EmailMessage emailMessage) {

    MimeMessagePreparator preparedMessage = new MimeMessagePreparator() {

        public void prepare(MimeMessage mimeMessage) throws Exception {

            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);

            message.setTo(emailMessage.getToEmail());
            message.setFrom(emailMessage.getFromAddress());
            message.setReplyTo(emailMessage.getFromAddress());
            message.setSubject(emailMessage.getSubject());
            String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
                    emailMessage.getTemplatePath(), "UTF-8", emailMessage.getModel());

            message.setText(body, true);

            if (!StringUtils.isBlank(emailMessage.getAttachmentPath())) {
                FileSystemResource file = new FileSystemResource(emailMessage.getAttachmentPath());
                message.addAttachment(emailMessage.getAttachmentName(), file);
            }
        }
    };

    try {
        this.mailSender.send(preparedMessage);
    } catch (MailException e) {
        logger.error("Email Service Exception Send Mime Mail: " + e.getMessage(), e);
    }

}

From source file:uk.org.funcube.fcdw.server.email.VelocityTemplateEmailSender.java

@Override
public void sendEmailUsingTemplate(final String fromAddress, final String toAddress,
        final String[] bccAddresses, final String subject, final String templateLocation,
        final Map<String, Object> model) {

    final Map<String, Object> augmentedModel = new HashMap<String, Object>(model);
    augmentedModel.put("dateTool", new DateTool());
    augmentedModel.put("numberTool", new NumberTool());
    augmentedModel.put("mathTool", new MathTool());

    final Writer writer = new StringWriter();
    VelocityEngineUtils.mergeTemplate(velocityEngine, templateLocation, augmentedModel, writer);
    final String emailBody = writer.toString();

    final MimeMessagePreparator prep = new MimeMessagePreparator() {
        @Override//from  w w w  . j a v a2  s .  co  m
        public void prepare(final MimeMessage mimeMessage) throws Exception {
            final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, EMAIL_CONTENT_ENCODING);
            message.setTo(toAddress);
            message.setFrom(fromAddress);
            message.setSubject(subject);
            message.setText(emailBody);

            if (!ArrayUtils.isEmpty(bccAddresses)) {
                message.setBcc(bccAddresses);
            }
        }
    };

    try {
        mailSender.send(prep);
        LOGGER.debug(String.format("Sent %3$s email To: %1$s, Bcc: %2$s", toAddress,
                ArrayUtils.toString(bccAddresses, "None"), templateLocation));
    } catch (final MailException e) {
        LOGGER.error("Could not send email " + subject, e);
        throw e;
    }
}

From source file:org.jrecruiter.service.notification.impl.DefaultNotificationServiceImpl.java

/** {@inheritDoc} */
@Override/*from   w  ww. j  a  v a 2s.c  om*/
public void sendEmail(final String email, final String subject, final Map context, final String templateName) {

    final MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws MessagingException, IOException {

            final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            message.setFrom("no_reply@jrecruiter.org");
            message.setTo(email);
            message.setSubject(subject);

            final Locale locale = LocaleContextHolder.getLocale();

            final Template textTemplate = freemarkerConfiguration.getTemplate(templateName + "-text.ftl",
                    locale);

            final StringWriter textWriter = new StringWriter();
            try {
                textTemplate.process(context, textWriter);
            } catch (TemplateException e) {
                throw new MailPreparationException("Can't generate email body.", e);
            }

            final Template htmlTemplate = freemarkerConfiguration.getTemplate(templateName + "-html.ftl",
                    locale);

            final StringWriter htmlWriter = new StringWriter();
            try {
                htmlTemplate.process(context, htmlWriter);
            } catch (TemplateException e) {
                throw new MailPreparationException("Can't generate email body.", e);
            }

            message.setText(textWriter.toString(), htmlWriter.toString());

        }
    };

    mailSender.send(preparator);
}

From source file:io.lavagna.model.MailConfig.java

public void send(final String to, final String subject, final String text, final String html) {

    toMailSender().send(new MimeMessagePreparator() {
        @Override/*w  w w .  j  a  va  2s.  c  o  m*/
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = html == null ? new MimeMessageHelper(mimeMessage, "UTF-8")
                    : new MimeMessageHelper(mimeMessage, true, "UTF-8");
            message.setSubject(subject);
            message.setFrom(getFrom());
            message.setTo(to);
            if (html == null) {
                message.setText(text, false);
            } else {
                message.setText(text, html);
            }
        }
    });
}