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

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

Introduction

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

Prototype

public void setTo(String[] to) throws MessagingException 

Source Link

Usage

From source file:edu.mum.emailservice.EmailService.java

public void sendOrderReceivedMail(final String recipientName, final String recipientEmail, Order order,
        String documentName, final Locale locale) throws MessagingException {

    // Prepare the evaluation context
    final Context thymeContext = new Context(locale);
    thymeContext.setVariable("name", recipientName);
    thymeContext.setVariable("order", order);

    // Prepare message using a Spring helper
    final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
    message.setSubject("Order Details");

    // could have CC, BCC, will also take an array of Strings
    message.setTo(recipientEmail);

    // Create the HTML body using Thymeleaf
    final String htmlContent = this.templateEngine.process("orderReceivedMail", thymeContext);
    message.setText(htmlContent, true /* isHtml */);

    message.addInline("imtheguy", new ClassPathResource(IM_THE_GUY), JPG_MIME);

    String documentLocation = "templates/images/" + documentName;
    message.addAttachment(documentName, new ClassPathResource(documentLocation));

    /* //from ww w.  j  a v  a 2s. c  o  m
       // Alternative
            File file = new File(documentLocation);
          message.addAttachment(documentName, file);
    */
    // Send email
    this.mailSender.send(mimeMessage);

}

From source file:com.ushahidi.swiftriver.core.mail.EmailHelper.java

private MimeMessagePreparator getMimeMessagePreparator(final User user, final String subject,
        final String mailBody) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {

        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper mimeHelper = new MimeMessageHelper(mimeMessage, true);
            mimeHelper.setFrom(senderAddress);
            mimeHelper.setTo(user.getEmail());
            mimeHelper.setReplyTo(senderAddress);
            mimeHelper.setSubject(subject);
            mimeHelper.setText(mailBody, true);
        }//ww w .j a v  a2s.  co  m
    };

    return preparator;
}

From source file:org.brushingbits.jnap.email.Email.java

public void prepare(MimeMessage mimeMessage) throws Exception {
    final EmailAccountInfo acc = getAccountInfo();
    boolean multipart = StringUtils.isNotBlank(getHtmlText())
            || (getInlineResources() != null && getInlineResources().size() > 0)
            || (getAttachments() != null && getAttachments().size() > 0);
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, multipart);
    helper.setTo(getTo());
    if (getCc() != null) {
        helper.setCc(getCc());//from  w w  w .ja  v  a2 s  .c o m
    }
    if (getBcc() != null) {
        helper.setBcc(getBcc());
    }
    helper.setSentDate(new Date());
    helper.setSubject(i18nTextProvider.getText(getSubject()));

    // sender info
    //      if (acc != null && StringUtils.isNotBlank(acc.getFromName())) {
    //         helper.setFrom(getFrom(), i18nTextProvider.getText(acc.getFromName()));
    //      } else {
    //         helper.setFrom(getFrom());
    //      }
    if (acc != null && StringUtils.isNotBlank(acc.getReplyToEmailAddress())) {
        if (StringUtils.isNotBlank(acc.getReplyToName())) {
            helper.setReplyTo(acc.getReplyToEmailAddress(), acc.getReplyToName());
        } else {
            helper.setReplyTo(acc.getReplyToEmailAddress());
        }
    }

    final boolean hasHtmlText = StringUtils.isNotBlank(getHtmlText());
    final boolean hasText = StringUtils.isNotBlank(getText());
    if (hasHtmlText && hasText) {
        helper.setText(getText(), getHtmlText());
    } else if (hasHtmlText || hasText) {
        helper.setText(hasHtmlText ? getHtmlText() : getText());
    }

    // add inline resources
    final Map<String, Resource> inlineRes = this.getInlineResources();
    if (inlineRes != null) {
        for (String cid : inlineRes.keySet()) {
            helper.addInline(cid, inlineRes.get(cid));
        }
    }
    // add attachments
    final Map<String, Resource> attachments = this.getAttachments();
    if (attachments != null) {
        for (String attachmentName : attachments.keySet()) {
            helper.addAttachment(attachmentName, attachments.get(attachmentName));
        }
    }
}

From source file:com.apress.progwt.server.service.impl.InvitationServiceImpl.java

public void sendInvite(final MailingListEntry invitation) throws InfrastructureException {
    // send mail// w w w. jav  a  2  s. c  o m
    try {
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                message.setTo(invitation.getEmail());
                message.setFrom(from);
                message.setSubject("ToCollege.net Invitation");

                Map<String, Object> model = new HashMap<String, Object>();
                model.put("inviter", invitation.getInviter());
                model.put("randomkey", invitation.getRandomkey());
                model.put("email", invitation.getEmail());

                Template textTemplate = configurer.getConfiguration().getTemplate(invitationTemplate);
                final StringWriter textWriter = new StringWriter();

                textTemplate.process(model, textWriter);

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

                log.info("Inviting: " + invitation.getEmail());
                log.debug("From: " + from);
                log.debug("Message: " + textWriter.toString());

            }
        };
        this.mailSender.send(preparator);

        invitation.setSentEmailOk(true);
        mailingListDAO.save(invitation);

    } catch (Exception e) {
        log.error(e);
        throw new InfrastructureException(e);
    }
}

From source file:com.marc.lastweek.business.services.mail.impl.MailServiceImpl.java

private MimeMessagePreparator getMimeMessagePreparator(final Locale locale, final String templateName,
        final Map<String, Object> templateData, final String mailTo) {

    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws MessagingException {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);

            // Set message attributes
            message.setTo(mailTo);
            message.setFrom(MailServiceImpl.this.from);
            message.setSubject(getMailMessageEntry(locale, templateName + FIELD_SUBJECT));

            // Add parameters
            Map<String, Object> model = new HashMap<String, Object>();
            model.put("locale", locale);
            model.put("dateTool", new DateTool());
            model.put("resourceTool", new ResourceTool());

            // Insert data in the template
            for (String name : templateData.keySet()) {
                model.put(name, templateData.get(name));
            }//from w  w  w.ja  v  a  2  s.c  om

            String text = VelocityEngineUtils.mergeTemplateIntoString(MailServiceImpl.this.velocityEngine,
                    MailServiceImpl.this.velocityTemplates.get(templateName), CHARSET, model);
            message.setText(text, true);

            // Insert stylesheet
            //ClassPathResource stylesheet = new ClassPathResource("templates/email.css", MailServiceImpl.class);
            //                ClassPathResource stylesheet = new ClassPathResource(MAIL_STYLESHEET, );
            //                message.addInline("email.css", stylesheet, "text/css");                        
        }
    };
    return preparator;
}

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

/** {@inheritDoc} */
@Override//from   ww  w. j a v a  2  s.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:edu.mum.eureka.email.EmailService.java

public void sendOrderReceivedMail(final String recipientName, final String recipientEmail, Order order,
        final Locale locale) throws MessagingException {

    // Prepare the evaluation context
    final Context thymeContext = new Context(locale);
    thymeContext.setVariable("name", recipientName);
    thymeContext.setVariable("order", order);

    // Prepare message using a Spring helper
    final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
    message.setSubject("Your  Eureka Order Details");

    // could have CC, BCC, will also take an array of Strings
    message.setTo(recipientEmail);

    // Create the HTML body using Thymeleaf
    final String htmlContent = this.templateEngine.process("orderReceivedMail", thymeContext);
    message.setText(htmlContent, true);// w w w .j  a  v a2  s  . c o  m

    message.addInline("imtheguy", new ClassPathResource(IM_THE_GUY), JPG_MIME);

    /*String documentLocation = "templates/images/" + documentName ;
     message.addAttachment(documentName, new ClassPathResource(documentLocation));*/

    /*
      // Alternative
           File file = new File(documentLocation);
         message.addAttachment(documentName, file);
    */
    // Send email
    this.mailSender.send(mimeMessage);

}

From source file:org.sharetask.service.MailServiceImpl.java

@Override
public void sendEmail(final String from, final List<String> to, final String subject, final String msg,
        final int retry) {
    final MimeMessage message = mailSender.createMimeMessage();
    final MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8");

    try {/* www. ja  v a 2 s.  c  o m*/
        helper.setFrom(noreplyMail);
        helper.setTo(to.toArray(new String[] {}));
        helper.setSubject(subject);
        helper.setText(msg, true /* html */);
        log.debug("Sending mail to:{} content:{}", to, msg);
        mailSender.send(message);
    } catch (final MailException ex) {
        log.error("Problem in sending email notification:", ex);
        notificationQueueService.storeInvitation(noreplyMail, to, subject, msg, retry + 1);
    } catch (final MessagingException e) {
        throw new IllegalStateException("Wrong mail message format: ", e);
    }
}

From source file:be.roots.taconic.pricingguide.service.MailServiceImpl.java

@Override
public void sendReport(DateTime lastMonth, String filename, byte[] report) throws MessagingException {

    final MimeMessageHelper helper = new MimeMessageHelper(javaMailSender.createMimeMessage(), true);

    helper.setFrom(fromEmail);// w  w  w  .  j  av  a 2s.c  om

    if (StringUtils.isEmpty(testEmail)) {
        helper.setTo(reportRecipientEmail.split(","));
    } else {
        helper.setTo(testEmail.split(","));
    }

    if (!StringUtils.isEmpty(bccEmail)) {
        helper.setBcc(bccEmail.split(","));
    }

    helper.setSubject(documentTitle + " requests for " + lastMonth.toString(DefaultUtil.FORMAT_MONTH));

    final String body = "Dear<br>" + "<br>" + "Attached you find the overview of " + documentTitle
            + " requests for " + lastMonth.toString(DefaultUtil.FORMAT_MONTH) + ".<br>" + "<br>"
            + "Taconic Biosciences, Inc.<br>" + "One Hudson City Centre<br>" + "Hudson, New York 12534<br>"
            + "North America +1 888 822-6642<br>" + "Europe +45 70 23 04 05<br>" + "info@taconic.com<br>"
            + "www.taconic.com";

    helper.setText(body, true);

    helper.addAttachment(filename, new ByteArrayResource(report));

    javaMailSender.send(helper.getMimeMessage());

}