JavaMailHtmlSendTest

需要的jar包:

mail-1.4.7.jar

maven配置:

<dependency>
    	<groupId>javax.mail</groupId>
    	<artifactId>mail</artifactId>
    	<version>1.4.7</version>
    </dependency>



package com.common.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ByteArrayInputStream;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;

import com.sun.mail.smtp.SMTPTransport;

/**
 * 邮件工具类 
 * 
 * @author chenlujun
 * @version [版本号, 2014-11-20]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class MailTools {
	/**
	 * Description:
	 * 
	 * @param args
	 *            [参数说明]
	 * 
	 * @return void [返回类型说明]
	 * @throws IOException
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public static void main(String[] args) throws IOException {
		String host="smtp.qq.com";  
        	String from="[email protected]";  
        	String to="[email protected]";  
        	String user="xxxxxxxxxx";  
        	String password="xxxxxxxxx";  
        	String prot = "smtp";  
        	String mailer = "sendhtml";  
		String context = "clj邮件内容20141120";
		boolean verbose = true;

		Properties props = new Properties();
		props.put("mail.smtp.host", host);
		props.put("mail.from", from);
		props.put("mail.smtp.auth", "true");
		Session session = Session.getInstance(props, null);

		try {

			Message msg = new MimeMessage(session);
			msg.setFrom();
			msg.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(to, false));
			msg.setSentDate(new Date());
			msg.setHeader("X-Mailer", mailer);

			msg.setSubject("邮件主题");

			ByteArrayInputStream bais = new ByteArrayInputStream(
					context.getBytes());
			BufferedReader in = new BufferedReader(new InputStreamReader(bais));
			collect(in, msg);

			SMTPTransport t = (SMTPTransport) session.getTransport(prot);
			try {
				t.connect(host, user, password);
				t.sendMessage(msg, msg.getAllRecipients());
			} finally {
				if (verbose) {
					System.out
							.println("Response: " + t.getLastServerResponse());
				}
				t.close();
			}

		} catch (MessagingException mex) {
			System.out.println("send failed, exception: " + mex);
		}
	}

	/**
	 * 拼接邮件内容
	 * 
	 * @param in 邮件正文内容
	 * @param msg 邮件消息体
	 * @throws MessagingException
	 * @throws IOException
	 * @see [类、类#方法、类#成员]
	 */
	public static void collect(BufferedReader in, Message msg)
			throws MessagingException, IOException {
		String line;
		String subject = msg.getSubject();
		StringBuffer sb = new StringBuffer();
		sb.append("<HTML>\n");
		sb.append("<HEAD>\n");
		sb.append("<TITLE>\n");
		sb.append(subject + "\n");
		sb.append("</TITLE>\n");
		sb.append("</HEAD>\n");

		sb.append("<BODY>\n");
		sb.append("<H1>" + subject + "</H1>" + "\n");

		while ((line = in.readLine()) != null) {
			sb.append(line);
			sb.append("\n");
		}

		sb.append("</BODY>\n");
		sb.append("</HTML>\n");

		msg.setDataHandler(new DataHandler(new ByteArrayDataSource(sb
				.toString(), "text/html")));
	}

}


你可能感兴趣的:(JavaMailHtmlSendTest)