<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.6</version>
</dependency>
拉到此处(下图所示)
如果是关闭的 请开启 然后就可以得到 16位SMTP
```java public void sendEmail(String email){ //创建Properties类 用于记录邮箱的一些属性 Properties props = new Properties();
//表示SMTP发送邮件 必须进行身份验证
props.put("mail.smtp.auth","true");
//此处填写SMTP服务器
props.put("mail.smtp.host","smtp.qq.com");
//端口号 QQ端口号 587
props.put("mail.smtp.port","587");
//此处填写写信人账号
props.put("mail.user","写信人账号");
//此处填写16位SMTP口令
props.put("mail.password","16位SMTP口令");
//构建授权信息 用于SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//用户名密码
String username = (String) props.get("mail.user");
String password = (String) props.get("mail.password");
return new PasswordAuthentication(username,password);
}
};
//使用环境属性和授权信息 创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
//创建邮件消息
MimeMessage mimeMessage = new MimeMessage(mailSession);
//设置发件人
InternetAddress form = null;
try {
form = new InternetAddress(props.get("mail.user").toString());
mimeMessage.setFrom(form);
InternetAddress to = new InternetAddress(email);
mimeMessage.setRecipients(Message.RecipientType.TO, String.valueOf(to));
//设置邮件标题
mimeMessage.setSubject("标题");
//设置邮件内容体
String content = "内容"
mimeMessage.setContent(content,"text/html;charset=UTF8");
//发送邮件
Transport.send(mimeMessage);
redisTemplate.opsForValue().set(email,code,15L,TimeUnit.MINUTES);
} catch (MessagingException e) {
e.printStackTrace();
}
}