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();
}
}
Java Mail简单测试程序 发送HTML数据
最新推荐文章于 2021-02-12 14:05:59 发布