1. 引入spring-boot-starter-mail 依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2. 在application.yml配置邮箱基本信息
spring:
mail:
host: smtp.exmail.qq.com
port: 465
username: xxx@xx.com
password: xxxx
protocol: smtp
properties:
mail:
smtp:
auth: true
ssl:
enable: true
socketFactory:
class: com.sun.mail.util.MailSSLSocketFactory
fallback: false
3. 实现代码
@Autowired
JavaMailSender javaMailSender;
public void testSend() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("XXX@xxx.com"); //发送者邮箱地址 此地址一定要和yml邮箱一致
message.setTo("xxx@xxx.com"); //收件人邮箱地址
message.setSubject("测试主题");
message.setText("测试内容");
jms.send(message);
}
注意:
如果代码报:501 mail from address must be same as authorization user 错误 ;引起原因是yml中配置的邮箱地址和代码中message.setFrom(xx@xx.com);不一致导致;
到此这篇关于SpringBoot+JavaMailSender实现腾讯企业邮箱配置的文章就介绍到这了,更多相关SpringBoot JavaMailSender腾讯邮箱配置内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!