package com.li72.util;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
public class EmailUtil {
public static void sendEmail(String email,String content)throws Exception{
Properties pro=new Properties();
pro.setProperty("mail.transport.protocol", "smtp");
// pro.setProperty("mail.host", "localhost");
//pro.setProperty("mail.smtp.host", "smtp.163.com");
pro.setProperty("mail.smtp.host","smtp.163.com");
pro.setProperty("mail.smtp.auth", "true");
Session session=Session.getDefaultInstance(pro, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("15576967929@163.com","********");
}
});
Message msg=new MimeMessage(session);
msg.setFrom(InternetAddress.parse(MimeUtility.encodeText("15576967929@163.com"))[0]);
msg.setSubject("主题");
//msg.setRecipient(Message.RecipientType.TO, new InternetAddress(email));//收件人
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email,false));//收件人
//msg.setSentDate(new Date());
msg.setContent(content, "text/html,utf-8");
System.out.println(""+msg);
Transport.send(msg);
}
}
本文介绍了一个使用Java实现的邮件发送工具类,该工具通过SMTP协议利用163邮箱服务发送HTML格式的内容邮件。文中详细展示了如何配置邮件发送所需的属性,并通过Session和Transport实现了邮件的发送过程。
4829

被折叠的 条评论
为什么被折叠?



