您的位置:首页 > 编程语言 > Java开发

springboot 发送邮件

2018-05-05 23:51 597 查看
一、使用场景

发送邮件

二、参考文献

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);
}


4.5 注意

4.5.1 部分腾讯云服务器发送失败,一直报链接超时,原因是服务器禁用了25端口,解封即可

https://jingyan.baidu.com/album/0964eca247cb9b8284f5364a.html?picindex=1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring Spring Boot