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

/**
 * Convenience method for sending messages with attachments.
 * //from   ww  w . ja v  a 2 s.  c om
 * @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: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  ww.  j  a  v a  2 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:com.foilen.smalltools.email.EmailServiceSpring.java

@Override
public void sendHtmlEmail(String from, String to, String subject, String html) {

    try {/*from   w  w  w .  j  ava 2s. c o m*/
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(html, true);

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

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

@Override
public void sendTextEmail(String from, String to, String subject, String text) {

    try {//  w  ww. j av a  2  s.  c om
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text, false);

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

From source file:com.rinxor.cloud.service.mail.Mailer.java

public void sendMail(String dear, String content) {

    MimeMessage message = mailSender.createMimeMessage();

    try {/*  w w w  .  jav  a 2s  .  c  om*/
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom(simpleMailMessage.getFrom());
        helper.setTo(simpleMailMessage.getTo());
        helper.setSubject(simpleMailMessage.getSubject());
        helper.setText(String.format(simpleMailMessage.getText(), dear, content));

        //FileSystemResource file = new FileSystemResource("C:\\log.txt");
        //helper.addAttachment(file.getFilename(), file);
    } catch (MessagingException e) {
        throw new MailParseException(e);
    }
    mailSender.send(message);
}

From source file:org.musicrecital.service.MailEngine.java

/**
 * Convenience method for sending messages with attachments.
 * //from   ww  w  .java  2  s.  co  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);

    // use the default sending if no sender specified
    if (sender == null) {
        helper.setFrom(defaultFrom);
    } else {
        helper.setFrom(sender);
    }

    helper.setText(bodyText);
    helper.setSubject(subject);

    helper.addAttachment(attachmentName, resource);

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

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

/**
 * ??MIME?./*from   w  w  w .j  a  va 2 s . c o  m*/
 */
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:org.fuin.auction.command.server.base.MailManager.java

/**
 * Creates a welcome mail with a unique identifier to verify the email
 * address./*from w w  w  . ja  v a  2s.co  m*/
 * 
 * @param event
 *            Event to handle.
 */
@EventHandler
public final void handle(final UserCreatedEvent event) {

    if (LOG.isDebugEnabled()) {
        LOG.debug("SEND user created mail to " + event.getEmail() + " [securityToken='"
                + event.getSecurityToken() + "']");
    }

    final MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(final MimeMessage mimeMessage) throws Exception {
            final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setTo(event.getEmail().toString());
            message.setFrom(mailProperties.getProperty("sender"));
            final Map<String, String> varMap = new HashMap<String, String>();
            varMap.put("email", event.getEmail().toString());
            varMap.put("userName", event.getUserName().toString());
            varMap.put("securityToken", event.getSecurityToken().toString());
            final String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
                    "user-created-mail.vm", varMap);
            message.setText(text, 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 .  ja v  a2 s . c o  m*/
            message.setText(body, true);
        }
    };
    this.mailSender.send(preparator);

}

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);
            message.setFrom(new InternetAddress(adminEmail));
            message.setSubject(SUBJECT_USER);
            String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
                    "templates/notify-user.vm", "UTF-8", templateVariables);
            log.info(body);/* w  w  w. jav a 2s .  c o m*/
            message.setText(body, true);
        }
    };
    this.mailSender.send(preparator);

}