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

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

Introduction

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

Prototype

public void setText(String plainText, String htmlText) throws MessagingException 

Source Link

Document

Set the given plain text and HTML text as alternatives, offering both options to the email client.

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  a v  a2 s .  c  o  m*/
    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.rxx.common.util.MailUtil.java

/**
 * html//  w w  w .  j av a 2 s .  c o  m
 *
 * @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//from w w w .j  av a  2s .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 {//  w  w  w .j  a  va2  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/*w  w  w. j  a  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.//  w w 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:la.kosmos.app.EmailConfiguration.java

public boolean sendEmail(String subject, String email, String message) throws Exception {
    try {/*from ww w .  j a  v  a  2 s.c o m*/
        MimeMessage msg = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg);
        helper.setSubject(subject);
        helper.setTo(email);
        helper.setText("<p>" + message + "</p>", true);
        mailSender.send(msg);
        return Boolean.TRUE;
    } catch (MessagingException | MailException e) {
        throw e;
    }
}

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);/*  w w w.  j  av  a2s .c  o  m*/
        message.setSubject(subject);
        message.setText(body, html);
        message.setValidateAddresses(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);//from w  ww. j a v a  2s. c  o  m
        message.setSubject(subject);
        message.setText(body, html);
        message.setValidateAddresses(true);
    });
}

From source file:br.com.semanticwot.cd.infra.MailManager.java

public void sendNewPurchaseMail(SystemUser user, String emailTemplate) throws MessagingException {

    Object[] args = { user.getName(), user.getUsername() };
    MessageFormat fmt = new MessageFormat(emailTemplate);

    MimeMessage message = mailer.createMimeMessage();

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

    // use the true flag to indicate the text included is HTML
    helper.setText(fmt.format(args), true);

    System.out.println("Passei por aqui " + user.getUsername());

    //SimpleMailMessage email = new SimpleMailMessage();
    helper.setFrom(user.getUsername());/*w  w w  .  j  a  va2s  .  co  m*/

    // Somente por enquanto, que esta em teste. 
    // Em producao mudar para helper.setTo(user.getLogin());
    helper.setTo("notlian.junior@gmail.com");

    helper.setSubject("PSWoT Register");
    mailer.send(message);
}