正文开始前,分享阿里 P8 资深架构师吐血总结的 《Java 核心知识体系&面试资料.pdf》
阿里 P8 级资深架构师吐血总结的一份 Java 核心知识.pdf, 内容覆盖很广,Java 核心基础、Java 多线程、高并发、Spring、微服务、Netty 与 RPC、Zookeeper、Kafka、RabbitMQ、Habase、设计模式、负载均衡、分布式缓存、Hadoop、Spark、Storm、云计算等。
获取方式:【关注 + 转发】后,【私信】我,回复关键字【资源】,即可免费无套路获取哦~
以下是资源的部分目录以及内容截图:
重要的事再说一遍,获取方式:【关注 + 转发】后,【私信】我,回复关键字【资源】,即可免费无套路获取哦~
开始正文,Show Time !!!
大家好,后续会间断地奉上一些 Spring Boot 2.x 相关的博文,包括 Spring Boot 2.x 教程和 Spring Boot 2.x 新特性教程相关,如 WebFlux 等。还有自定义 Starter 组件的进阶教程,比如:如何封装一个自定义图床 Starter 启动器(支持上传到服务器内部,阿里 OSS 和七牛云等), 仅仅需要配置相关参数,就可以将图片上传功能集成到现有的项目中。
好吧,这些都是后话。今天主要来讲讲如何在 Spring Boot 2.x 版本中集成发送邮件功能。
可以说,邮件发送在企业级应用中是比较常见的服务了,如运维报警,用户激活,广告推广等场景,均会使用到它。废话少说,开干!
目录
一、添加依赖
二、添加邮件相关配置
三、关于授权码
3.1 什么是 QQ 邮箱授权码3.2 如何获取
四、开始编码
4.1 定义功能类4.2 项目结构4.3 单元测试,验证效果
五、总结
一、添加依赖
在 pom.xml 文件中添加 spring-boot-starter-mail 依赖:
org.springframework.boot spring-boot-starter-mail
二、添加邮件相关配置
在 application.properties 配置文件中添加下面内容:
# 发送邮件的服务器,笔者这里使用的 QQ 邮件spring.mail.host=spring.mail.username=你的邮箱地址spring.mail.password=授权码,或邮箱密码spring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.starttls.required=true
yml 格式的配置文件,添加如下:
spring: mail: host: #发送邮件的服务器,笔者这里使用的 QQ 邮件 username: 你的邮箱地址 password: 授权码,或邮箱密码 properties.mail.smtp.auth: true properties.mail.smtp.starttls.enable: true default-encoding: utf-8
三、关于授权码
对于上面的配置,您肯定对密码配置那块还抱有疑问,如果您使用的是 163 邮箱,或者 Gmail 邮箱,直接使用密码就可以了,如果您使用的是 QQ 邮箱,则需要先获取授权码。
3.1 什么是 QQ 邮箱授权码
下图截自 QQ 邮箱官方文档:
3.2 如何获取
登录 QQ 邮箱:
点击设置:
跳转页面后,点击账户,将页面往下拖动,您会看到:
验证成功过后,即可获取授权码:
四、开始编码
4.1 定义功能类
先定义一个邮件服务的接口类, MailService.java:
package site.exception.springbootmail.service;/** * * @site 个人网站: www.exception.site * @date /4/10 * @time 下午4:19 * @discription **/public interface MailService { /** * 发送简单文本的邮件 * @param to * @param subject * @param content * @return */ boolean send(String to, String subject, String content); /** * 发送 html 的邮件 * @param to * @param subject * @param html * @return */ boolean sendWithHtml(String to, String subject, String html);/** * 发送带有图片的 html 的邮件 * @param to * @param subject * @param html * @param cids * @param filePaths * @return */ boolean sendWithImageHtml(String to, String subject, String html, String[] cids, String[] filePaths); /** * 发送带有附件的邮件 * @param to * @param subject * @param content * @param filePaths * @return */ boolean sendWithWithEnclosure(String to, String subject, String content, String[] filePaths);}
接口内定义了四个方法:
send(): 发送简单文本的邮件;sendWithHtml(): 发送 html 的邮件;sendWithImageHtml(): 发送带有图片的 html 的邮件;sendWithWithEnclosure: 发送带有附件的邮件;
完成接口的定义以后,我们再定义一个具体实现类, MailServiceImpl.java:
package site.exception.springbootmail.service.impl;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.mail.MailProperties;import org.springframework.core.io.FileSystemResource;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Service;import org.springframework.util.Assert;import site.exception.springbootmail.service.MailService;import javax.mail.internet.MimeMessage;/** * * @site 个人网站: www.exception.site * @date /4/10 * @time 下午4:19 * @discription **/@Servicepublic class MailServiceImpl implements MailService { private final static Logger logger = LoggerFactory.getLogger(MailServiceImpl.class); @Autowired private MailProperties mailProperties; @Autowired private JavaMailSender javaMailSender; /** * 发送简单文本的邮件 * @param to * @param subject * @param content * @return */ @Override public boolean send(String to, String subject, String content) { logger.info("## Ready to send mail ..."); SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); // 邮件发送来源 simpleMailMessage.setFrom(mailProperties.getUsername()); // 邮件发送目标 simpleMailMessage.setTo(to); // 设置标题 simpleMailMessage.setSubject(subject); // 设置内容 simpleMailMessage.setText(content);try { // 发送 javaMailSender.send(simpleMailMessage); logger.info("## Send the mail success ..."); } catch (Exception e) { logger.error("Send mail error: