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
protocol: smtp
port: 465
username: XXXX@xxx.com #邮箱地址
password: XXXXXXX #邮箱密码
properties:
mail:
smtp:
auth: true
ssl:
enable: true
socketFactory:
class: com.sun.mail.util.MailSSLSocketFactory
fallback: false
3. 实现代码
@Autowired
JavaMailSender jms;
public void testSend() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("XXX@xxx.com"); //发送者邮箱地址
message.setTo("haopeng@newbeescm.com"); //收件人邮箱地址
message.setSubject("测试主题");
message.setText("测试内容");
jms.send(message);
}
4. 注意事项
1. 开通腾讯企业邮箱后,必须修改原密码,否则会报发送异常(501 ÇëµÇ¼exmail.qq.comÐÞ¸ÄÃÜÂë );