com.rxx.common.util.MailUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.rxx.common.util.MailUtil.java

Source

/**
The MIT License (MIT) * Copyright (c) 2015 
    
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
    
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
    
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.rxx.common.util;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.internet.MimeMessage;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.MessagingException;

/**
 * 
 * 
 * 
 * <p>
 * <b></b>
 * </p>
 *
 * <p>
 * Copyright: Copyright (c) 2014 - 2015
 * </p>
 *
 * @author  QQ:78750478
 *
 *         <p>
 *         Comments:springmail,
 *         </p>
 *
 *         <p>
 *         Create Date:2014-11-4
 *         </p>
 *
 *         <p>
 *         Modification history:
 *         </p>
 */
public class MailUtil {

    /**
     * 
     *
     * @param host 
     * @param port
     * @param userName
     * @param password
     * @param title
     * @param content
     * @param toUser
     */
    public static void sendText(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);
        // 
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //  
        // String[] array = new String[] {"sun111@163.com","sun222@sohu.com"};
        // mailMessage.setTo(array);
        mailMessage.setTo(toUser);
        mailMessage.setFrom(userName);
        mailMessage.setSubject(title);
        mailMessage.setText(content);

        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);
    }

    /**
     * html
     *
     * @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();
        }

    }

    /**
     * html
     *
     * @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);
    }

    /**
     * html
     *
     * @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);
    }
}