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

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

Introduction

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

Prototype

public void setSubject(String subject) throws MessagingException 

Source Link

Document

Set the subject of the message, using the correct encoding.

Usage

From source file:edu.sjsu.cmpe275.project.service.EmailService.java

@Async
public void send(Reservation reservation, String text) {
    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = null;
    try {//from  ww  w .j  av a  2  s .co m
        helper = new MimeMessageHelper(message, true);
        helper.setTo(reservation.getEmail());
        helper.setReplyTo("cmpe275.mini.hotel@gmail.com");
        helper.setFrom("cmpe275.mini.hotel@gmail.com");
        helper.setSubject("Your hotel reservation has been confirmed");
        helper.setText(text, true);
        javaMailSender.send(message);
    } catch (MessagingException e) {
        e.printStackTrace();
    }
    return;
}

From source file:com.epam.ta.reportportal.util.email.EmailService.java

/**
 * Restore password email//from w  w  w .j  a v  a2s  .co m
 *
 * @param subject
 * @param recipients
 * @param url
 */
public void sendRestorePasswordEmail(final String subject, final String[] recipients, final String url,
        final String login) {
    MimeMessagePreparator preparator = mimeMessage -> {
        URL logoImg = this.getClass().getClassLoader().getResource(LOGO);
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "utf-8");
        message.setSubject(subject);
        message.setTo(recipients);

        setFrom(message);

        Map<String, Object> email = new HashMap<>();
        email.put("login", login);
        email.put("url", url);
        String text = templateEngine.merge("restore-password-template.vm", email);
        message.setText(text, true);
        message.addInline("logoimg", new UrlResource(logoImg));
    };
    this.send(preparator);
}

From source file:eu.trentorise.smartcampus.citizenportal.service.EmailService.java

public void sendMailWithInline(final String recipientName, final String recipientEmail,
        final String imageResourceName, final byte[] imageBytes, final String imageContentType,
        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"));
    ctx.setVariable("imageResourceName", imageResourceName); // so that we can reference it from HTML

    // 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 inline image");
    message.setFrom("thymeleaf@example.com");
    message.setTo(recipientEmail);//from   ww w  . jav  a 2s. co  m

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

    // Add the inline image, referenced from the HTML code as "cid:${imageResourceName}"
    final InputStreamSource imageSource = new ByteArrayResource(imageBytes);
    message.addInline(imageResourceName, imageSource, imageContentType);

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

}

From source file:net.bafeimao.umbrella.web.test.MailTests.java

@Test
public void testSendHtmlMail() throws Exception {
    // ?,???html/*from  www.jav  a  2 s . com*/
    MimeMessage mailMessage = senderImpl.createMimeMessage();
    MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);

    // 
    messageHelper.setTo("29283212@qq.com");
    messageHelper.setFrom("29283212@qq.com");
    messageHelper.setSubject("HTML?");
    // true ?HTML?
    messageHelper.setText("<html><head></head><body><h1>hello!!spring html Mail</h1></body></html>", true);

    // ??
    senderImpl.send(mailMessage);

    System.out.println("???..");
}

From source file:gr.abiss.calipso.service.EmailService.java

@Async
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   w  w w  . ja va 2 s.  c o m

    // Create the HTML body using Thymeleaf
    final String htmlContent = this.templateEngine.process("email-withattachment.html", ctx);
    LOGGER.info("Sending email body: " + htmlContent);
    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:org.jrecruiter.service.notification.impl.DefaultNotificationServiceImpl.java

/** {@inheritDoc} */
@Override/*from   w w  w.  j  a  v  a 2 s .  c o  m*/
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:gr.abiss.calipso.service.EmailService.java

@Async
public void sendMailWithInline(final String recipientName, final String recipientEmail,
        final String imageResourceName, final byte[] imageBytes, final String imageContentType,
        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"));
    ctx.setVariable("imageResourceName", imageResourceName); // so that we can reference it from HTML

    // 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 inline image");
    message.setFrom("thymeleaf@example.com");
    message.setTo(recipientEmail);/* ww  w . j ava  2 s.  c  o  m*/

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

    // Add the inline image, referenced from the HTML code as "cid:${imageResourceName}"
    final InputStreamSource imageSource = new ByteArrayResource(imageBytes);
    message.addInline(imageResourceName, imageSource, imageContentType);

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

}

From source file:fr.mycellar.application.user.impl.ResetPasswordRequestServiceImpl.java

@Override
public void createAndSendEmail(User user, String url) {
    // Create request
    ResetPasswordRequest request = new ResetPasswordRequest();
    request.setDateTime(new LocalDateTime());
    request.setKey(new String(Base64.encodeBase64(secureRandom.generateSeed(128), false)).substring(0, 32));
    request.setUser(user);// w ww  .  j  a v a 2s .com
    // Merge it in repository
    resetPasswordRequestRepository.save(request);
    // Send email to email
    final String email = user.getEmail();
    final String address;
    try {
        address = url + "?key=" + URLEncoder.encode(request.getKey(), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("UTF-8 encoding not supported.", e);
    }

    MimeMessagePreparator mimeMessagePreparator = new MimeMessagePreparator() {
        @Override
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            helper.setTo(email);
            helper.setFrom(configurationService.getMailAddressSender());
            helper.setSubject("Changement de mot de passe");
            helper.setText("Allez  l'adresse suivante : " + address);
        }
    };
    try {
        javaMailSender.send(mimeMessagePreparator);
    } catch (Exception e) {
        throw new RuntimeException("Cannot send email.", e);
    }
}

From source file:br.ufc.ivela.commons.mail.MailService.java

public List<MimeMessage> send(String[] to, String from, String subject, String content)
        throws MessagingException {

    if (to == null || to.length <= 0) {
        throw new IllegalArgumentException("You cannot send an email without recipients");
    }//from w w  w . j a  va2s.  com

    if (from == null || from.isEmpty()) {
        throw new IllegalArgumentException("You cannot send an email without a sender");
    }

    if (subject == null || subject.isEmpty()) {
        throw new IllegalArgumentException("You cannot send an email without a subject");
    }

    content = content != null ? content : "";

    // Construct the message
    MimeMessage[] messagesToSend = new MimeMessage[to.length];

    for (int i = 0; i < to.length; i++) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        helper.setTo(to[i]);
        helper.setFrom(from);
        helper.setSubject(subject);
        helper.setText(content, true);
        messagesToSend[i] = message;
    }

    List<MimeMessage> messagesNotSent = new ArrayList<MimeMessage>(messagesToSend.length);

    if (disabled)
        return messagesNotSent;

    for (MimeMessage message : messagesToSend) {
        try {
            this.mailSender.send(message);
        } catch (Exception e) {
            log.error("Error sending Email to:" + message.toString(), e);
            messagesNotSent.add(message);
        }
    }

    return messagesNotSent;
}

From source file:eu.trentorise.smartcampus.citizenportal.service.EmailService.java

public String sendMailWithAttachment(final String recipientName, final String recipientEmail,
        final String practice_id, final String position, final String score, final String phase,
        final String ef_period, final String ef_category, final String ef_tool, final String subject,
        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("practice_id", practice_id);
    ctx.setVariable("position", position);
    ctx.setVariable("score", score);
    ctx.setVariable("phase", phase);
    ctx.setVariable("ef_period", ef_period);
    ctx.setVariable("ef_category", ef_category);
    ctx.setVariable("ef_tool", ef_tool);
    ctx.setVariable("subscriptionDate", new Date());
    //ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
    ctx.setVariable("text", subject);

    // Prepare message using a Spring helper
    final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8");
    message.setSubject("Graduatoria Edilizia Abitativa");
    //message.setFrom("thymeleaf@example.com");
    message.setFrom("myweb.edilizia@comunitadellavallagarina.tn.it");
    message.setTo(recipientEmail);//from   w w w.  j  av  a  2 s  .co m

    // 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);

    return recipientName + "OK";
}