目录
获取QQ 授权码
SpringBoot构建
依赖
Yaml配置
服务编写
测试
获取QQ 授权码
https://mail.qq.com/
接着后就会有对应的密钥了
SpringBoot构建
依赖
这里的建议是 2.0系列的Springboot版本用低一点的邮件依赖
<!-- 电子邮件 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId><version>2.7.18</version>
</dependency>
Yaml配置
spring:mail:default-encoding: UTF-8host: smtp.qq.comport: 587username: 你的邮件password: 刚刚QQ生成的密钥properties:mail:smtp:starttls:enable: trueauth: truesocketFactory:class: javax.net.ssl.SSLSocketFactorydebug: true
服务编写
为了方便直接写在 控制层 了
package com.takem.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class EmailController {@Autowiredprivate JavaMailSender mailSender;@GetMapping("/email")public String sendSimpleMessage() {SimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setFrom("自己的邮箱");mailMessage.setTo("别人的邮箱");mailMessage.setText("这是内容");mailMessage.setSubject("这是邮件标题");mailSender.send(mailMessage);return "====完成发送!====";}
}
测试
以自己发给自己为例