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:com.gisgraphy.service.MailEngine.java

/**
 * Convenience method for sending messages with attachments.
 * //from w  w  w .jav  a  2 s  .  c  o m
 * @param recipients
 *                array of e-mail addresses
 * @param sender
 *                e-mail address of sender
 * @param resource
 *                attachment from classpath
 * @param bodyText
 *                text in e-mail
 * @param subject
 *                subject of e-mail
 * @param attachmentName
 *                name for attachment
 * @throws MessagingException
 *                 thrown when can't communicate with SMTP server
 */
public void sendMessage(String[] recipients, String sender, ClassPathResource resource, String bodyText,
        String subject, String attachmentName) throws MessagingException {
    MimeMessage message = ((JavaMailSenderImpl) mailSender).createMimeMessage();

    // use the true flag to indicate you need a multipart message
    MimeMessageHelper helper = new MimeMessageHelper(message, true);

    helper.setTo(recipients);
    helper.setFrom(sender);
    helper.setText(bodyText);
    helper.setSubject(subject);

    helper.addAttachment(attachmentName, resource);

    ((JavaMailSenderImpl) mailSender).send(message);
}

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);//w  ww .ja va 2 s . c  o 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);

}

From source file:com.aurora.mail.service.MailService.java

/**
 * @param to/*from   w  w  w  .  j a v a  2s . co  m*/
 * @param subject
 * @param content
 * @param isMultipart
 * @param isHtml
 */
@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
    log.debug("Send e-mail to user '{}'!", to);

    // Prepare message using a Spring helper
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
        message.setTo(to);
        message.setFrom(from);
        message.setSubject(subject);
        message.setText(content, isHtml);
        javaMailSender.send(mimeMessage);
        log.debug("Sent e-mail to user '{}'!", to);
    } catch (Exception e) {
        log.error("E-mail could not be sent to user '{}', exception is: {}", to, e.getMessage());
    }
}

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

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

    // 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 editable HTML email");
    message.setFrom("thymeleaf@example.com");
    message.setTo(recipientEmail);//  w  w  w .  j  a  v  a  2s.  c o  m

    // FIXME: duplicated images in src/main/resources and src/main/webapp
    // 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"));

    final StaticTemplateExecutor templateExecutor = new StaticTemplateExecutor(ctx, messageResolver,
            HTML5.getTemplateModeName());
    final String output = templateExecutor.processTemplateCode(htmlContent);
    message.setText(output, true /* isHtml */);

    // Add the inline images, referenced from the HTML code as "cid:image-name"
    message.addInline("background", new ClassPathResource(BACKGROUND_IMAGE), PNG_MIME);
    message.addInline("logo-background", new ClassPathResource(LOGO_BACKGROUND_IMAGE), PNG_MIME);
    message.addInline("thymeleaf-banner", new ClassPathResource(THYMELEAF_BANNER_IMAGE), PNG_MIME);
    message.addInline("thymeleaf-logo", new ClassPathResource(THYMELEAF_LOGO_IMAGE), PNG_MIME);

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

}

From source file:me.j360.base.service.common.MimeMailService.java

/**
 * ??MIME?./*from   w ww. ja  v  a 2  s . com*/
 */
public void sendNotificationMail(String userName) {

    try {
        MimeMessage msg = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg, true, DEFAULT_ENCODING);

        helper.setTo("xuminwlt2008@163.com");
        helper.setFrom("system@smart-sales.cn");
        helper.setSubject("");

        String content = generateContent(userName);
        helper.setText(content, true);

        File attachment = generateAttachment();
        helper.addAttachment("mailAttachment.txt", attachment);

        mailSender.send(msg);
        logger.info("HTML??xuminwlt2008@163.com");
    } catch (MessagingException e) {
        logger.error("", e);
    } catch (Exception e) {
        logger.error("??", e);
    }
}

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

private MimeMessage createMessage(String to, String from, String subject, String velocityTemplate, Map params)
        throws MessagingException {
    MimeMessage mime = mailSender.createMimeMessage();
    MimeMessageHelper message = new MimeMessageHelper(mime);
    message.setTo(to);/*from  ww w  . j  av a 2  s . c  o m*/
    message.setFrom(from);
    message.setSubject(subject);
    String text = velocityTemplate != null && !velocityTemplate.isEmpty()
            ? VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, velocityTemplate, params)
            : "";
    message.setText(text, true);

    return mime;
}

From source file:org.crce.interns.service.impl.EmailNotificationServiceImpl.java

/**
 * /*from  w  w w.j  a v a2s . co m*/
 * @param receivers
 * @param category
 * @param message 
 */
public void sendEmailNotification(String receivers, String category, String message) {

    if (receivers.equalsIgnoreCase(COMPS) || receivers.equalsIgnoreCase(IT) || receivers.equalsIgnoreCase(PROD)
            || receivers.equalsIgnoreCase(ELEX)) {
        String list = sendEmailDAO.fetchStreamStudents(receivers);
    } else {

        //indiwala
        intendedReceivers = intendedReceivers.concat(sendEmailDAO.fetchStudentEmailId(receivers));
    }

    String[] emailIds = intendedReceivers.split("\\s");

    javaMailSender.send(new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage)
                throws javax.mail.MessagingException, IllegalStateException, IOException {
            System.out.println("Throws Exception");
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

            //mimeMessageHelper.setTo(request.getParameter("receiver"));
            mimeMessageHelper.setTo(emailIds);

            mimeMessageHelper.setSubject(category + "-Unread Notification-Placement Management System-CRCE");

            mimeMessageHelper.setText(message);

        }
    });

}

From source file:com.aurora.mail.service.MailService.java

/**
 * @param to/*from ww w. j a va  2 s .  c o m*/
 * @param subject
 * @param content
 * @param isMultipart
 * @param isHtml
 */
@Async
public void sendEmailWithAttachment(String to, String subject, String content, final String attachmentFileName,
        final byte[] attachmentBytes, final String attachmentContentType, boolean isMultipart, boolean isHtml) {
    log.debug("Send e-mail to user '{}'!", to);

    final InputStreamSource attachmentSource = new ByteArrayResource(attachmentBytes);

    // Prepare message using a Spring helper
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
        message.setTo(to);
        message.setFrom(from);
        message.setSubject(subject);
        message.setText(content, isHtml);

        // Add the attachment
        message.addAttachment(attachmentFileName, attachmentSource, attachmentContentType);
        javaMailSender.send(mimeMessage);
        log.debug("Sent e-mail to User '{}'!", to);
    } catch (Exception e) {
        log.error("E-mail could not be sent to user '{}', exception is: {}", to, e.getMessage());
    }
}

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);//  ww  w.j a v a 2  s  .c om
            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:com.oak_yoga_studio.service.impl.NotificationServiceImpl.java

public void sendMail(String fromEmail, String toEmail, String emailSubject, String emailBody) {

    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try {/*from  www  . ja v a 2s. com*/
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

        helper.setFrom(fromEmail);
        helper.setTo(toEmail);
        helper.setSubject(emailSubject);
        helper.setText(emailBody);

        /*
         uncomment the following lines for attachment FileSystemResource
         file = new FileSystemResource("attachment.jpg");
         helper.addAttachment(file.getFilename(), file);
         */
        javaMailSender.send(mimeMessage);
        System.out.println("Mail sent successfully.");//debugging
    } catch (MessagingException e) {
        e.printStackTrace();
    }

}