Springboot 实现发送邮件
1. QQ邮箱服务器,授权码申请
在发送邮件之前,我们先需要申请QQ邮箱的授权码。如图所示:
设置 -> 账户 -> 生成授权码
2. 代码开发
代码目录
2.1 新建工程,导入依赖
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.mail</groupId> <artifactId>demo-mail</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-mail</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.0.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
2.2 代码实现
SendMailService
package com.example.mail.demomail.service; /** * @author wy * @version 1.0 * @date 2020/7/2 11:06 * @description **/ public interface SendMailService { /** * 发送普通文本邮件 * * @param to 收件人 * @param subject 主题 * @param content 内容 */ void sendSimpleMail(String to, String subject, String content); }
MailServiceimpl
package com.example.mail.demomail.service.impl; import com.example.mail.demomail.service.SendMailService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; /** * @author wy * @version 1.0 * @date 2020/7/2 11:06 * @description **/ @Service @Slf4j public class MailServiceimpl implements SendMailService { @Autowired private JavaMailSender mailSender; @Value("${spring.mail.username}") private String from; @Override public void sendSimpleMail(String to, String subject, String content) { log.info("begin send mail"); SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to);//收信人 message.setSubject(subject);//主题 message.setText(content);//内容 message.setFrom(from);//发信人 log.info("send mail success"); mailSender.send(message); } }
SenderVO
package com.example.mail.demomail.controller.vo; import lombok.Data; /** * @author wy * @version 1.0 * @date 2020/7/2 11:14 * @description **/ @Data public class SenderVO { //收信人 private String to; //主题 private String subject; //内容 private String content; }
SendMailCntroller
package com.example.mail.demomail.controller; import com.example.mail.demomail.controller.vo.SenderVO; import com.example.mail.demomail.service.SendMailService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; /** * @author wy * @version 1.0 * @date 2020/7/2 11:12 * @description **/ @RestController public class SendMailCntroller { @Autowired private SendMailService sendMailService; @GetMapping("/") @ResponseBody public String get(){ return "it works"; } @PostMapping("/sender") public void sendMail(@RequestBody SenderVO vo){ sendMailService.sendSimpleMail(vo.getTo(),vo.getSubject(),vo.getContent()); } }
application.yml
spring: mail: host: smtp.qq.com username: ****@qq.com password: ****** default-encoding: UTF-8