public class EmailDemo {
public static void main(String[] args) {
// 先登录QQ邮箱服务器
// 创建会话之前,设置一些参数
Properties properties = new Properties();
// 设置key 和 value
/*
* 指定连接的邮件服务器的主机名。如:qq邮箱就填写smtp.qq.com
* 若在本地测试的话,需要在本地安装smtp服务器
*/
properties.setProperty("mail.host","smtp.qq.com");
// 发送邮件,采用的协议
/*
* 指定邮件发送协议:smtp。smtp:发邮件;pop3:收邮件
* mail.store.protocol:指定邮件接收协议
*/
properties.setProperty("mail.transport.protocol","smtp");
// 会话
Session session = Session.getDefaultInstance(properties);
Transport ts = null;
try {
// 获取到传输对象 发送邮件
ts = session.getTransport();
// 发送邮件之前,校验账号和授权码
try {
// zwwojvc*****eadf
ts.connect("22*****840@qq.com","zwwojvc*****eadf");
// 构建一封邮件
MimeMessage message = new MimeMessage(session);
// 发件人
message.setFrom(new InternetAddress("22*****840@qq.com"));
// 收件人
message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("37****372@qq.com")});
// 抄送
message.setRecipients(Message.RecipientType.CC, new InternetAddress[]{new InternetAddress("w**com@qq.com")});
message.setRecipients(Message.RecipientType.CC, new InternetAddress[]{new InternetAddress("9***32857@qq.com")});
message.setRecipients(Message.RecipientType.CC, new InternetAddress[]{new InternetAddress("7****2886@qq.com")});
// 主题
message.setSubject("爸妈不在家,免费升级VIP,手慢无");
// 设置正文
message.setContent("您好,由于您之前长期使用本网站观看视频,我们为了答谢老用户,特此举办本次免费升级VIP活动,<a href='http://www.ne***ian.com/meinv/'>点击领取</a>即可!","text/html;charset=UTF-8");
// 保存邮件
message.saveChanges();
// 发送邮件
ts.sendMessage(message,message.getAllRecipients());
} catch (MessagingException e) {
e.printStackTrace();
}
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
}
}
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
添加maven依赖