一、使用场景
发送邮件
二、参考文献
2.1 https://blog.csdn.net/u011244202/article/details/54809696
说明:本文复制的2.1 ,用作备份,已实践过,但是springboot2.0.1.RELEASE并未遇到2.1描述的535错误
三、测试环境
jdk8、springboot2.0.1.RELEASE
四、使用
4.1 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
4.2 配置文件(application.properties)
# JavaMailSender 邮件发送的配置
spring.mail.host=smtp.163.com
spring.mail.username=用户163邮箱
spring.mail.password=邮箱密码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
注意:若使用QQ邮箱发送邮件,则需要修改为spring.mail.host=smtp.qq.com
,同时spring.mail.password
改为QQ邮箱的授权码。
QQ邮箱->设置->账户->POP3/SMTP服务:开启服务后会获得QQ的授权码
4.3 代码
@Autowired
private JavaMailSender mailSender; //自动注入的Bean
@Value("${spring.mail.username}")
private String Sender; //读取配置文件中的参数
public void sendSimpleMail() throws Exception {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(Sender);
message.setTo(Sender);
message.setSubject("主题");
message.setText("内容");
mailSender.send(message);
}