谷歌邮箱配置:
首先登录邮箱 进入设置 如下
开启二步验证
进入二次验证,底部有个应用专用密码,进入设置
代码配置:
首先导入依赖
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
配置项 yml
### email begin
mail:
host: smtp.gmail.com
username: xxx@gmail.com ### 邮箱地址
password: xxxxx ### 应用专用密码
protocol: smtp
properties.mail.smtp.auth: true
properties.mail.smtp.port: 465
properties.mail.display.sendmail: xxx@gmail.com
properties.mail.display.sendname: xxx@gmail.com
properties.mail.smtp.starttls.enable: true
properties.mail.smtp.starttls.required: true
properties.mail.smtp.ssl.enable: true
from: xxx@gmail.com
default-encoding: utf-8
#### email end
代码块
@Value("${spring.mail.from}")
private String from;
@Resource
private JavaMailSender mailSender;
/**
* 发送文本邮件
* @param to
* @param subject
*/
@Override
public ResultBean sendSimpleMail(String to, String subject,String contentStr) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from); //配置邮箱
message.setTo(to); //收件邮箱
message.setSubject(subject); //邮件标题
message.setText(contentStr); //邮件内容
try{
mailSender.send(message);
}catch (Exception e){
e.printStackTrace();
}
return new ResultBean();
}