springboot 发送邮件

放肆的年华 提交于 2020-12-22 06:48:16

一、使用场景

发送邮件

二、参考文献

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!