Java Mail简单测试程序 发送HTML数据

import java.util.Properties;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class JavaMailTest {
	public static void main(String[] args) throws MessagingException {
		String email = "123@qq.com";
		Properties props=new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol","smtp");
		
		Session session1 =Session.getDefaultInstance(props);
		session1.setDebug(true);
		
		Message message=new MimeMessage(session1);
		message.setSubject("您的密码已经重置完成");
		BodyPart bodyPart = new MimeBodyPart();
		String strText = " <style type= 'text/css'>"+       
				"a:HOVER {"+
				"text-decoration:underline;"+ 
				"color:red;"+
				"}"+
				"#div1 {"+
				"width:640px;"+
				"}"+
				"#div2 {"+
				"text-align: right;"+
				"}"+
				"</style> "+
				"<body>"+"<div id='div1'>"+
				"亲爱的##您好,您的密码已经成功被重置为:123123 , 新密码即时生效。"+"<br/>"+"请登录"+
				"<a href='##'>"+"##"+"</a>"+
				"进入“平台服务”,修改密码。这是系统发出的通知,请不要直接回复。"+"<br/>"+
				"如有其它疑问,请访问"+"<a href='##'>"+"##"+"</a>"+"获得更多帮助。并请详细说明您遇到的情况,避免发生不必要的损失!"+"<div id='div2'>"+
				"版权所有 NSSD.ORG"+"</div>"+"</div>"+
				"</body>";
		bodyPart.setContent( " <meta http-equiv='Content-Type' content='text/html; charset=utf-8 '> "+strText, "text/html;charset=utf-8 ");//set   the   type   and   encoding 
		
		Multipart mm = new MimeMultipart(); 
		mm.addBodyPart(bodyPart); 
		message.setContent(mm); 
		
		message.setSentDate(new java.util.Date(System.currentTimeMillis()+3600*8000));
		message.setRecipient(Message.RecipientType.TO,new InternetAddress("##@163.com"));
		message.setFrom(new InternetAddress("##@163.com"));
		
		
		Transport transport=session1.getTransport();
		transport.connect("smtp.163.com", 25, "##@163.com","***");
		transport.sendMessage(message, new Address[]{new InternetAddress(email)});
		transport.close();
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值