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.netsteadfast.greenstep.util.MailClientUtils.java

public static void send(String from, String to, String cc[], String bcc[], String fileNames[], File files[],
        String subject, String text) throws MailException, Exception {

    if (mailSender == null) {
        throw new Exception("null mailSender!");
    }// w w w .j av a 2  s  .  com
    if (StringUtils.isBlank(from) || StringUtils.isBlank(to)) {
        throw new Exception("from and to is required!");
    }
    if (fileNames != null && files != null) {
        if (fileNames.length != files.length) {
            throw new Exception("File parameter error!");
        }
    }
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true, Constants.BASE_ENCODING);
    helper.setFrom(from);
    helper.setTo(to.endsWith(";") ? to.substring(0, to.length() - 1) : to);
    helper.setSubject(subject);
    helper.setText(text, true);
    if (null != cc && cc.length > 0) {
        helper.setCc(cc);
    }
    if (null != bcc && bcc.length > 0) {
        helper.setBcc(bcc);
    }
    for (int i = 0; fileNames != null && i < fileNames.length; i++) {
        helper.addAttachment(fileNames[i], files[i]);
    }
    mailSender.send(message);
}

From source file:com.mocktpo.util.EmailUtils.java

public static void sendActivationCode(License lic) {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setHost(GlobalConstants.SMTP_HOST);
    sender.setUsername(GlobalConstants.SMTP_SENDER_EMAIL);
    sender.setPassword(GlobalConstants.SMTP_SENDER_PASSWORD);
    MimeMessage message = sender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message);
    try {//from w  ww.  j  a v  a2s . co m
        helper.setFrom(GlobalConstants.SMTP_SENDER_EMAIL);
        helper.setBcc(GlobalConstants.SMTP_SENDER_EMAIL);
        helper.setTo(lic.getEmail());
        helper.setSubject(GlobalConstants.LICENSE_EMAIL_SUBJECT);
        helper.setText(getActivationCode(lic));
    } catch (Exception e) {
        e.printStackTrace();
    }
    sender.send(message);
}

From source file:com.rxx.common.util.MailUtil.java

/**
 * html// w w  w  .  j  a v a 2 s . c  om
 *
 * @param host 
 * @param port
 * @param userName
 * @param password
 * @param title
 * @param contenthtml
 * @param imgs
 * @param toUser
 * @throws javax.mail.MessagingException
 */
public static void sendNews(String host, int port, String userName, String password, String title,
        String content, List<String> imgs, String[] toUser)
        throws MessagingException, javax.mail.MessagingException {
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
    // mail server
    senderImpl.setHost(host);

    // ,html
    MimeMessage mailMessage = senderImpl.createMimeMessage();
    // boolean,MimeMessageHelpertrue
    // multipart
    MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);

    // 
    messageHelper.setTo(toUser);
    messageHelper.setFrom(userName);
    messageHelper.setSubject(title);
    // true HTML
    messageHelper.setText(content, true);

    int i = 0;
    for (String imagePath : imgs) {
        FileSystemResource img = new FileSystemResource(new File(imagePath));
        messageHelper.addInline(i + "", img);
        i++;
    }

    senderImpl.setUsername(userName); // ,username
    senderImpl.setPassword(password); // , password
    Properties prop = new Properties();
    prop.put("mail.smtp.auth", "true"); // true,
    prop.put("mail.smtp.timeout", "25000");
    senderImpl.setJavaMailProperties(prop);
    // 
    senderImpl.send(mailMessage);

    // 
    senderImpl.send(mailMessage);
}

From source file:com.rxx.common.util.MailUtil.java

/**
 * html//  ww w .ja v  a2  s.  c  o m
 *
 * @param host 
 * @param port
 * @param userName
 * @param password
 * @param title
 * @param contenthtml
 * @param fileslist<Map<key:,value:>>
 * @param toUser
 * @throws javax.mail.MessagingException
 */
public static void sendAttached(String host, int port, String userName, String password, String title,
        String content, List<Map<String, String>> files, String[] toUser)
        throws MessagingException, javax.mail.MessagingException {
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

    // mail server
    senderImpl.setHost(host);
    // ,html
    MimeMessage mailMessage = senderImpl.createMimeMessage();
    // boolean,MimeMessageHelpertrue
    // multipart true html
    MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");

    // 
    messageHelper.setTo(toUser);
    messageHelper.setFrom(userName);
    messageHelper.setSubject(title);
    // true HTML
    messageHelper.setText(content, true);

    for (Map<String, String> filePath : files) {
        Iterator<String> it = filePath.keySet().iterator();
        String fileName = it.next();
        FileSystemResource file = new FileSystemResource(new File(filePath.get(fileName)));
        // 
        messageHelper.addAttachment(fileName, file);
    }

    senderImpl.setUsername(userName); // ,username
    senderImpl.setPassword(password); // , password
    Properties prop = new Properties();
    prop.put("mail.smtp.auth", "true"); // true,
    prop.put("mail.smtp.timeout", "25000");
    senderImpl.setJavaMailProperties(prop);
    // 
    senderImpl.send(mailMessage);
}

From source file:julie.com.mikaelson.util.EmailSenderUtil.java

synchronized public static boolean sendEmail(String[] toList, String content) {
    boolean result = false;
    MimeMessage message = javaMail.createMimeMessage();
    MimeMessageHelper messageHelp;
    try {/*from w w w. ja v  a2 s.  com*/
        long start = System.currentTimeMillis();
        System.out.println("sendEmail invoked ");
        messageHelp = new MimeMessageHelper(message, true, "UTF-8");
        messageHelp.setFrom(emailConfig.getFrom());
        messageHelp.setTo(toList);
        messageHelp.setSubject("Test");
        String body = content + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        messageHelp.setText(body, true);
        javaMail.send(message);
        System.out.println("send total time is" + (System.currentTimeMillis() - start));
        Thread.sleep(10 * 1000l);

    } catch (MailException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:com.rxx.common.util.MailUtil.java

/**
 * html/*from ww  w  . ja v a  2  s  . c o  m*/
 *
 * @param host 
 * @param port
 * @param userName
 * @param password
 * @param title
 * @param contenthtml
 * @param toUser
 * @throws javax.mail.MessagingException
 */
public static void sendHtml(String host, int port, String userName, String password, String title,
        String content, String[] toUser) {
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
    // mail server
    senderImpl.setHost(host);
    senderImpl.setPort(port);
    // ,html
    MimeMessage mailMessage = senderImpl.createMimeMessage();

    try {
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "UTF-8");

        try {
            // 
            messageHelper.setTo(toUser);
            messageHelper.setFrom(userName);
            messageHelper.setSubject(title);
            // true HTML
            messageHelper.setText(content, true);
        } catch (javax.mail.MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        senderImpl.setUsername(userName); // ,username
        senderImpl.setPassword(password); // , password
        Properties prop = new Properties();
        prop.put("mail.smtp.auth", "true"); // true,
        prop.put("mail.smtp.timeout", "25000");
        senderImpl.setJavaMailProperties(prop);
        // 
        senderImpl.send(mailMessage);
    } catch (javax.mail.MessagingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

}

From source file:org.apache.niolex.commons.mail.EmailUtil.java

/**
 * Send an email.//  ww  w  .  j a v a  2 s  . c o  m
 * ????
 *
 * @param from the email sender
 * @param tos the email receivers array
 * @param ccs the carbon copiers array
 * @param title the email title
 * @param text the email body
 * @param attachments the email attachments list
 * @param priority priority from 1-5 the smaller is higher
 * @param isHtml is the text in HTML format or not
 * @param encoding the encoding of email, i.e. "GBK"?"UTF-8"
 * @throws MailException
 * @throws MessagingException
 */
public static void sendMail(String from, String[] tos, String[] ccs, String title, String text,
        List<Pair<String, InputStreamSource>> attachments, String priority, boolean isHtml, String encoding)
        throws MailException, MessagingException {
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, encoding);

    messageHelper.setFrom(from);
    messageHelper.setBcc(from);

    if (ArrayUtils.isEmpty(tos)) {
        throw new IllegalArgumentException("<tos> can not be null or empty!");
    } else {
        messageHelper.setTo(tos);
    }

    if (!ArrayUtils.isEmpty(ccs)) {
        messageHelper.setCc(ccs);
    }

    messageHelper.setSubject(title);
    messageHelper.setText(text, isHtml);

    if (attachments != null) {
        for (Pair<String, InputStreamSource> pair : attachments) {
            messageHelper.addAttachment(pair.a, pair.b);
        }
    }

    mimeMessage = messageHelper.getMimeMessage();
    if (priority != null) {
        mimeMessage.addHeader("X-Priority", priority);
    }

    mailSender.send(mimeMessage);
}

From source file:com.yoncabt.ebr.executor.YoncaMailSender.java

public void send(String to, String text, Map<String, byte[]> attachments) throws MessagingException {

    MimeMessage mm = mailSender.createMimeMessage();
    MimeMessageHelper mmh = new MimeMessageHelper(mm, true);
    mmh.setTo(to);
    mmh.setText(text);//  w  w  w .  j a va  2s.com
    for (Map.Entry<String, byte[]> entrySet : attachments.entrySet()) {
        String key = entrySet.getKey();
        byte[] value = entrySet.getValue();
        ByteArrayResource isr = new ByteArrayResource(value);
        mmh.addAttachment(key, isr);

    }
    mailSender.send(mm);
}

From source file:org.khmeracademy.btb.auc.pojo.utilities.Feedback_service.java

public boolean sendFeedback(User_feedback user) throws MailException, MessagingException {

    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper mail = new MimeMessageHelper(message, true);

    mail.setTo("kuylim.auction@gmail.com");
    mail.setFrom("kuylim.auction@gmail.com");
    mail.setSubject(user.getSubject());/*  w  ww.j av a2  s .  c  o  m*/

    mail.setText("<html>" + "<body>" + "<p>Dear, K-Auction</p>" + "<p>" + user.getComment() + "</p>"
            + "<p>Regard,</p>" + "<p>" + user.getName() + "</p>" + "<p> phone: " + user.getPhone() + " email: "
            + user.getEmail() + "</p>" + "</body>" + "</html>", true);

    javaMailSender.send(message);
    return true;
}

From source file:edu.pitt.dbmi.ccd.mail.AbstractBasicMail.java

public void send(String to, String subject, String body, boolean html) throws MessagingException {
    javaMailSender.send(mimeMessage -> {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
        message.setTo(to);
        message.setSubject(subject);// ww w. j a  v a  2s  .  c om
        message.setText(body, html);
        message.setValidateAddresses(true);
    });
}