Example usage for org.springframework.mail.javamail MimeMessageHelper setText

List of usage examples for org.springframework.mail.javamail MimeMessageHelper setText

Introduction

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

Prototype

public void setText(String plainText, String htmlText) throws MessagingException 

Source Link

Document

Set the given plain text and HTML text as alternatives, offering both options to the email client.

Usage

From source file:thymeleafexamples.springmail.service.EmailService.java

public void sendSimpleMail(final String recipientName, final String recipientEmail, final Locale locale)
        throws MessagingException {

    // Prepare the evaluation context
    final Context ctx = new Context(locale);
    ctx.setVariable("name", recipientName);
    ctx.setVariable("subscriptionDate", new Date());
    ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));

    // Prepare message using a Spring helper
    final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
    message.setSubject("Example HTML email (simple)");
    message.setFrom("thymeleaf@example.com");
    message.setTo(recipientEmail);//from w w  w . j a va 2  s  . com

    // Create the HTML body using Thymeleaf
    final String htmlContent = this.templateEngine.process("email-simple.html", ctx);
    message.setText(htmlContent, true /* isHtml */);

    // Send email
    this.mailSender.send(mimeMessage);

}

From source file:cz.zcu.kiv.eegdatabase.data.service.SpringJavaMailService.java

protected MimeMessage createMimeMessage(String from, String to, String subject, String emailBody)
        throws MailException {
    try {// w  w w .ja v a  2 s  .c o  m
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
        message.setFrom(from);
        // message.setContent("text/html");
        message.setTo(to);
        message.setSubject(subject);
        message.setText(emailBody, true);
        log.debug("Message " + message);
        return mimeMessage;
    } catch (MessagingException e) {// rethrow as MailException
        throw new MailPreparationException(e.getMessage(), e);
    }
}

From source file:org.cgiar.dapa.ccafs.tpe.service.impl.TPEMailService.java

@Override
public void notifyUser(final String userEmail, final Map<String, Object> templateVariables) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
            message.setTo(userEmail);//w  w w . j  ava  2s. co m
            message.setFrom(new InternetAddress(adminEmail));
            message.setSubject(SUBJECT_USER);
            String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
                    "templates/notify-user.vm", "UTF-8", templateVariables);
            log.info(body);
            message.setText(body, true);
        }
    };
    this.mailSender.send(preparator);

}

From source file:org.cgiar.dapa.ccafs.tpe.service.impl.TPEMailService.java

@Override
public void contactUs(final Map<String, Object> templateVariables) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
            message.setTo(supportEmail);
            message.setFrom(new InternetAddress(adminEmail));
            message.setSubject(SUBJECT_ADMIN);
            String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "templates/contact-us.vm",
                    "UTF-8", templateVariables);
            log.info(body);/*from w w  w  .  ja  v a2s  . co  m*/
            message.setText(body, true);
        }
    };
    this.mailSender.send(preparator);

}

From source file:org.cgiar.dapa.ccafs.tpe.service.impl.TPEMailService.java

@Override
public void notifyAdmin(final Map<String, Object> templateVariables) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
            message.setTo(supportEmail);
            message.setFrom(new InternetAddress(adminEmail));
            message.setSubject(SUBJECT_ADMIN);
            String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
                    "templates/notify-admin.vm", "UTF-8", templateVariables);
            log.info(body);/*from ww w.j a  va 2 s.  co m*/
            message.setText(body, true);
        }
    };
    this.mailSender.send(preparator);

}

From source file:com.cfitzarl.cfjwed.service.impl.EmailDispatchingServiceImpl.java

/** {@inheritDoc} **/
@Override/*ww w. ja v a2s . c  o  m*/
public void send(String to, String subject, String template, Map<String, Object> attrs) {
    // Add message source decorator to attributes list for localization
    attrs.put("localeSource", localizationService);

    String htmlFilename = String.format("/email-templates/%s-html.vm", template);
    String textFilename = String.format("/email-templates/%s-text.vm", template);

    String htmlContent = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, htmlFilename, "UTF-8",
            attrs);
    String textContent = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, textFilename, "UTF-8",
            attrs);

    try {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        String fromEmail = configDao.findByKey(ConfigKey.EMAIL).getValue();
        String fromTitle = configDao.findByKey(ConfigKey.TITLE).getValue();

        String from = String.format("%s <%s>", fromTitle, fromEmail);

        helper.setTo(to);
        helper.setSubject(subject);
        helper.setFrom(from);
        helper.setReplyTo(from);
        helper.setText(textContent, htmlContent);

        taskExecutor.submit(new MailRunner(message, javaMailSender));
    } catch (MessagingException e) {
        LOGGER.error("Error generating mail message", e);
    }
}

From source file:gr.abiss.calipso.mail.MailSender.java

/**
 * //from w ww  .jav a2 s. co  m
 * @param email
 * @param subject
 * @param messageBody
 * @param attachments
 * @param encryption
 * @param keyStorePath
 */
public void send(String email, String subject, String messageBody, Map<String, DataSource> attachments,
        boolean html) {
    // prepare message
    try {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        if (html) {
            helper.setText(addHeaderAndFooter(messageBody), true);
        } else {
            helper.setText(messageBody, false);
        }
        helper.setSubject(subject);
        helper.setSentDate(new Date());
        helper.setFrom(from);
        // set TO
        helper.setTo(email);
        if (MapUtils.isNotEmpty(attachments)) {
            for (String attachmentFilename : attachments.keySet()) {
                DataSource in = attachments.get(attachmentFilename);
                if (in != null) {
                    helper.addAttachment(attachmentFilename, in);
                }
            }
        }

        //logger.info("Sending email: "+subject+"\n"+messageBody);
        sendInNewThread(message);
    } catch (Exception e) {
        logger.error("failed to prepare e-mail", e);
    }
}

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);
        }//from  w  w  w  . j  av a2  s .co m
    };
    this.mailSender.send(preparator);
}

From source file:thymeleafexamples.springmail.service.EmailService.java

public void sendMailWithAttachment(final String recipientName, final String recipientEmail,
        final String attachmentFileName, final byte[] attachmentBytes, final String attachmentContentType,
        final Locale locale) throws MessagingException {

    // Prepare the evaluation context
    final Context ctx = new Context(locale);
    ctx.setVariable("name", recipientName);
    ctx.setVariable("subscriptionDate", new Date());
    ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));

    // Prepare message using a Spring helper
    final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8");
    message.setSubject("Example HTML email with attachment");
    message.setFrom("thymeleaf@example.com");
    message.setTo(recipientEmail);//from   www . j a  v a 2 s.  c  om

    // Create the HTML body using Thymeleaf
    final String htmlContent = this.templateEngine.process("email-withattachment.html", ctx);
    message.setText(htmlContent, true /* isHtml */);

    // Add the attachment
    final InputStreamSource attachmentSource = new ByteArrayResource(attachmentBytes);
    message.addAttachment(attachmentFileName, attachmentSource, attachmentContentType);

    // Send mail
    this.mailSender.send(mimeMessage);

}

From source file:com.foilen.smalltools.email.EmailServiceSpring.java

@Override
public void sendEmail(EmailBuilder emailBuilder) {

    try {//from w  w w  . ja  va  2  s.  c o  m
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(emailBuilder.getFrom());
        for (String to : emailBuilder.getTos()) {
            helper.addTo(to);
        }
        for (String cc : emailBuilder.getCcs()) {
            helper.addCc(cc);
        }
        for (String bcc : emailBuilder.getBccs()) {
            helper.addBcc(bcc);
        }
        helper.setSubject(emailBuilder.getSubject());
        helper.setText(emailBuilder.getBody(), emailBuilder.isHtml());

        // Inline
        for (EmailAttachment emailAttachment : emailBuilder.getInlineAttachments()) {
            helper.addInline(emailAttachment.getId(), emailAttachment.getResource());
        }

        // Attachment
        for (EmailAttachment emailAttachment : emailBuilder.getAttachments()) {
            helper.addAttachment(emailAttachment.getId(), emailAttachment.getResource());
        }

        mailSender.send(message);
    } catch (Exception e) {
        throw new SmallToolsException("Could not send email", e);
    }
}