相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送,在Spring Boot的Starter模块中也为此提供了自动化配置。
下面通过实例来讲解如何在Spring Boot中使用JavaMailSender发送邮件。
什么是SMTP?
SMTP全称为Simple Mail Transfer Protocol(简单邮件传输协议),它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。SMTP认证要求必须提供账号和密码才能登陆服务器,其设计目的在于避免用户受到垃圾邮件的侵扰。
什么是POP3?
POP3全称为Post Office Protocol 3(邮局协议),POP3支持客户端远程管理服务器端的邮件。POP3常用于“离线”邮件处理,即允许客户端下载服务器邮件,然后服务器上的邮件将会被删除。目前很多POP3的邮件服务器只提供下载邮件功能,服务器本身并不删除邮件,这种属于改进版的POP3协议。
传输协议
SMTP协议
发送邮件:我们通常把处理用户smtp请求(邮件发送请求)的服务器称之为SMTP服务器(邮件发送服务器)。
POP3协议
接收邮件:我们通常把处理用户pop3请求(邮件接收请求)的服务器称之为POP3服务器(邮件接收服务器)。
进阶知识
- 什么是JavaMailSender和JavaMailSenderImpl?
JavaMailSender和JavaMailSenderImpl 是Spring官方提供的集成邮件服务的接口和实现类,以简单高效的设计著称,目前是Java后端发送邮件和集成邮件服务的主流工具。
- 如何通过JavaMailSenderImpl发送邮件?‘
非常简单,直接在业务类注入JavaMailSenderImpl并调用send方法发送邮件。其中简单邮件可以通过SimpleMailMessage来发送邮件,而复杂的邮件(例如添加附件)可以借助MimeMessageHelper来构建MimeMessage发送邮件。
e,g:- @Autowired
- private JavaMailSenderImpl mailSender;
- public void sendMail() throws MessagingException {
- //简单邮件
- SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
- simpleMailMessage.setFrom("admin@163.com");
- simpleMailMessage.setTo("socks@qq.com");
- simpleMailMessage.setSubject("Happy New Year");
- simpleMailMessage.setText("新年快乐!");
- mailSender.send(simpleMailMessage);
- //复杂邮件
- MimeMessage mimeMessage = mailSender.createMimeMessage();
- MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
- messageHelper.setFrom("admin@163.com");
- messageHelper.setTo("socks@qq.com");
- messageHelper.setSubject("Happy New Year");
- messageHelper.setText("新年快乐!");
- messageHelper.addInline("doge.gif", new File("xx/xx/doge.gif"));
- messageHelper.addAttachment("work.docx", new File("xx/xx/work.docx"));
- mailSender.send(mimeMessage);
- }
复制代码
- 为什么JavaMailSenderImpl 能够开箱即用 ?
所谓开箱即用其实就是基于官方内置的自动配置,翻看源码可知晓邮件自动配置类(MailSenderPropertiesConfiguration) 为上下文提供了邮件服务实例(JavaMailSenderImpl)。具体源码如下:
- @Configuration
- @ConditionalOnProperty(prefix = "spring.mail", name = "host")
- class MailSenderPropertiesConfiguration {
- private final MailProperties properties;
- MailSenderPropertiesConfiguration(MailProperties properties) {
- this.properties = properties;
- }
- @Bean
- @ConditionalOnMissingBean
- public JavaMailSenderImpl mailSender() {
- JavaMailSenderImpl sender = new JavaMailSenderImpl();
- applyProperties(sender);
- return sender;
- }
复制代码 其中MailProperties是关于邮件服务器的配置信息,具体源码如下:- @ConfigurationProperties(prefix = "spring.mail")
- public class MailProperties {
- private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
- private String host;
- private Integer port;
- private String username;
- private String password;
- private String protocol = "smtp";
- private Charset defaultEncoding = DEFAULT_CHARSET;
- private Map<String, String> properties = new HashMap<>();
- }
复制代码 实现
1.开启邮件服务
这里以QQ邮箱为例。
首先登录QQ邮箱>>>登录成功后找到设置>>>然后找到邮箱设置>>>点击账户>>>找到POP3|SMTP服务>>>点击开启(开启需要验证,验证成功后会有一串授权码用于发送邮件使用)>>>验证成功
<img alt="image-20221008154945207" loading="lazy">
引入依赖
在springboot项目中,引入如下依赖:- <?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>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.7.4</version>
- <relativePath/>
- </parent>
- <groupId>com.galaxy</groupId>
- <artifactId>MailSender</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>MailSender</name>
- <description>MailSender</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-mail</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.10</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
复制代码 application.yml配置
在配置文件application.yml文件中写入发送邮件的配置信息- # 服务端口
- server:
- port: 8083
- spring:
- #邮箱基本配置
- mail:
- #配置smtp服务主机地址
- # qq邮箱为smtp.qq.com 端口号465或587
- # sina smtp.sina.cn
- # aliyun smtp.aliyun.com
- # 163 smtp.163.com 端口号465或994
- host: smtp.qq.com
- #发送者邮箱
- username: 896668626@qq.com
- #配置密码,注意不是真正的密码,而是刚刚申请到的授权码
- password: gltiixjllkakxobfjd
- #端口号465或587
- port: 587
- #默认的邮件编码为UTF-8
- default-encoding: UTF-8
- #其他参数
- properties:
- mail:
- #配置SSL 加密工厂
- smtp:
- ssl:
- #本地测试,先放开ssl
- enable: false
- required: false
- #开启debug模式,这样邮件发送过程的日志会在控制台打印出来,方便排查错误
- debug: true
复制代码 注:配置 SpringBoot 启动端口还可以通过IDEA 本身的配置来指定启动端口
- 第一步

- 第二步

编写发送邮件方法
编写邮件业务类MailService.
主要通过MailService工具类就可以满足发送java邮件的需要。当我们进行好 yml 配置后,SpringBoot会帮助我们自动配置 JavaMailSender 我们通过这个java类就可以实现操作java来发送邮件。
发送纯文本邮件
service
- package com.galaxy.mailsender.service;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.mail.javamail.JavaMailSenderImpl;
- import org.springframework.mail.javamail.MimeMessageHelper;
- import org.springframework.stereotype.Service;
- import javax.mail.MessagingException;
- import java.util.Date;
- /**
- * 邮件业务类
- * @author Galaxy
- */
- @Service
- public class MailService {
- /**
- * 注入邮件工具类
- */
- @Autowired
- private JavaMailSenderImpl javaMailSender;
- @Value("${spring.mail.username}")
- private String sendMailer;
- /**
- * 检测邮件信息类
- * @param to
- * @param subject
- * @param text
- */
- private void checkMail(String to,String subject,String text){
- if(StringUtils.isEmpty(to)){
- throw new RuntimeException("邮件收信人不能为空");
- }
- if(StringUtils.isEmpty(subject)){
- throw new RuntimeException("邮件主题不能为空");
- }
- if(StringUtils.isEmpty(text)){
- throw new RuntimeException("邮件内容不能为空");
- }
- }
- /**
- * 发送纯文本邮件
- * @param to
- * @param subject
- * @param text
- */
- public void sendTextMailMessage(String to,String subject,String text){
- try {
- //true 代表支持复杂的类型
- MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(),true);
- //邮件发信人
- mimeMessageHelper.setFrom(sendMailer);
- //邮件收信人 1或多个
- mimeMessageHelper.setTo(to.split(","));
- //邮件主题
- mimeMessageHelper.setSubject(subject);
- //邮件内容
- mimeMessageHelper.setText(text);
- //邮件发送时间
- mimeMessageHelper.setSentDate(new Date());
- //发送邮件
- javaMailSender.send(mimeMessageHelper.getMimeMessage());
- System.out.println("发送邮件成功:"+sendMailer+"->"+to);
- } catch (MessagingException e) {
- e.printStackTrace();
- System.out.println("发送邮件失败:"+e.getMessage());
- }
- }
- }
复制代码 Controller
- package com.galaxy.mailsender.controller;
- import com.galaxy.mailsender.service.MailService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class MailController {
- @Autowired
- private MailService mailService;
- /**
- * 发送文本邮件
- * @param to
- * @param subject
- * @param text
- */
- @RequestMapping("/sendTextMail")
- public void sendTextMail(String to,String subject,String text){
- mailService.sendTextMailMessage(to,subject,text);
- }
- }
复制代码 测试


结果

补充
发送html邮件
Spring Boot支持使用HTML发送邮件是通过MimeMessage来完成的。
发送带附件的邮件
MimeMessageHelper支持发送复杂邮件模板,支持文本、附件、HTML、图片等。比如需要发送附件,则在上面的代码中通过调用helper的addAttachment(fileName, file)方法即可。
例:发送HTML邮件
- package com.example.emaildemo.controller;
- import com.example.emaildemo.service.MailService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- /**
- * 测试邮件发送
- * @author galaxy
- */
- @RestController
- public class SendMailController {
- @Autowired
- private MailService mailService;
- /**
- * 发送HTML邮件
- * @param to
- * @param subject
- * @param content
- */
- @RequestMapping("/sendHtmlMailMessage")
- public void sendHtmlMailMessage(String to,String subject,String content){
- mailService.sendHtmlMailMessage(to,subject,content);
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |