1. 发件人邮箱需要开通SMTP协议
qq邮箱:https://jingyan.baidu.com/article/6079ad0eb14aaa28fe86db5a.html
网易邮箱:http://help.163.com/10/0312/13/61J0LI3200752CLQ.html
2. 给项目添加maven依赖
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
3. 代码实现
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;
public class EmailSendUtil {
// 邮箱服务器
private String host;
private String username;
// 邮箱密码,若设置了授权码就填写授权码
private String password;
private Address[] mail_to;
private String mail_from;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Address[] getMail_to() {
return mail_to;
}
public void setMail_to(Address[] mail_to) {
this.mail_to = mail_to;
}
public String getMail_from() {
return mail_from;
}
public void setMail_from(String mail_from) {
this.mail_from = mail_from;
}
public EmailSendUtil() {
}
/**
*
* @param host 发送邮箱的服务器的smtp地址
* @param username 发件人用户名
* @param password 发件人密码或授权码
* @param mailto 收件人列表
*/
public EmailSendUtil(String host, String username, String password,
Address[] mailto) {
this.host = host;
this.username = username;
this.mail_from = username;
this.password = password;
this.mail_to = mailto;
}
/**
* 此段代码用来发送普通电子邮件
*
* @throws MessagingException
* @throws UnsupportedEncodingException
* @throws GeneralSecurityException
*/
public void send() throws Exception {
Properties props = new Properties();
// 进行邮件服务器用户认证
Authenticator auth = new Email_Autherticator();
// mail.smtp.host :设置发送邮件的服务器
props.setProperty("mail.smtp.host", host);
// 授权
props.setProperty("mail.smtp.auth", "true");
// 设置邮件发送的协议
props.setProperty("mail.transport.protocol", "smtp");
// ssl设置
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.setProperty("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
// 设置session和邮件服务器进行通讯
Session session = Session.getDefaultInstance(props, auth);
// 编辑邮件内容信息
MimeMessage message = createMimeMessage(session,mail_from,mail_to);
// 发送邮件
Transport.send(message);
}
/**
* 用来进行服务器对用户的认证
*/
public class Email_Autherticator extends Authenticator {
public Email_Autherticator() {
super();
}
public Email_Autherticator(String user, String pwd) {
super();
username = user;
password = pwd;
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
/**
* 创建一封只包含文本的简单邮件
*
* @param session session
* @param sendMail 发件人邮箱
* @param receiveMail 收件人邮箱
* @return
* @throws Exception
*/
public MimeMessage createMimeMessage(Session session, String sendMail, Address[] receiveMail) throws Exception {
// 1. 创建一封邮件
MimeMessage message = new MimeMessage(session);
// 2. From: 发件人
message.setFrom(new InternetAddress(sendMail, "昵称", "UTF-8"));
/*
3. To: 收件人(可以增加多个收件人、抄送、密送)
若只发给单个用户:message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "XX用户", "UTF-8"));
*/
message.setRecipients(MimeMessage.RecipientType.TO,receiveMail);
// 4. Subject: 邮件主题
message.setSubject("主题", "UTF-8");
// 5. Content: 邮件正文(内容里可以使用html标签),例如<h1>湿度超过阀值</h1>
message.setContent("湿度超过阀值", "text/html;charset=UTF-8");
// 6. 设置发件时间,下面表示现在就发送
message.setSentDate(new Date());
// 7. 设置邮件标题
message.setHeader("告警名称", "湿度告警");
// 8. 保存设置
message.saveChanges();
return message;
}
/**
* 测试方法
* @throws Exception
*/
public void test() throws Exception {
//new 收件人
Address[] mailto = new Address[2];
/**
* new InternetAddress()中的第一个参数为收件人邮箱地址,第二个参数为收件人名称,第三个为邮件格式
*/
mailto[0] = new InternetAddress("******@163.com","某某","UTF-8");
mailto[1] = new InternetAddress("*****@qq.com","某某的另一个邮箱","UTF-8");
EmailSendUtil sendEmail = new EmailSendUtil("smtp.qq.com",
"******@qq.com", "*******", mailto);
try {
sendEmail.send();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("finished");
}
}
4. 可能出现的问题
STMP错误码:https://blog.csdn.net/weixin_42615847/article/details/103579588
我在写的时候遇到了:javax.mail.AuthenticationFailedException: 535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´ 这个问题,按理来说,535应该是权限校验失败,但是我的账号和授权码都是对的,测了好几次,后来用Authenticator把用户名和密码封装了一下,然后就没有这个问题了。写代码的过程中还是要多确认一下,注意细节。