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="xxxxxxxxxx@qq.com";  
        	String to="xxxxxxxxxx@qq.com";  
        	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")));
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值