Spring Boot 2.0 图文教程 | 集成邮件发送功能

文章首发自个人微信公众号: 小哈学Java
个人网站: https://www.exception.site/springboot/spring-boots-send-mail

大家好,后续会间断地奉上一些 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 单元测试,验证效果

五、总结

六、GitHub 源码地址

一、添加依赖

pom.xml 文件中添加 spring-boot-starter-mail 依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

二、添加邮件相关配置

application.properties 配置文件中添加下面内容:

# 发送邮件的服务器,笔者这里使用的 QQ 邮件
spring.mail.host=smtp.qq.com
spring.mail.username=你的邮箱地址
spring.mail.password=授权码,或邮箱密码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

yml 格式的配置文件,添加如下:

spring:
  mail:
    host: smtp.qq.com #发送邮件的服务器,笔者这里使用的 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;

/**
 * @author 犬小哈(微信号: 小哈学Java)
 * @site 个人网站: www.exception.site
 * @date 2019/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);

}

接口内定义了四个方法:

  1. send(): 发送简单文本的邮件;
  2. sendWithHtml(): 发送 html 的邮件;
  3. sendWithImageHtml(): 发送带有图片的 html 的邮件;
  4. 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;

/**
 * @author 犬小哈(微信号: 小哈学Java)
 * @site 个人网站: www.exception.site
 * @date 2019/4/10
 * @time 下午4:19
 * @discription
 **/
@Service
public 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: ", e);
            return false;
        }

        return true;
    }

    /**
     * 发送 html 的邮件
     * @param to
     * @param subject
     * @param html
     * @return
     */
    @Override
    public boolean sendWithHtml(String to, String subject, String html) {
        logger.info("## Ready to send mail ...");
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = null;
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            // 邮件发送来源
            mimeMessageHelper.setFrom(mailProperties.getUsername());
            // 邮件发送目标
            mimeMessageHelper.setTo(to);
            // 设置标题
            mimeMessageHelper.setSubject(subject);
            // 设置内容,并设置内容 html 格式为 true
            mimeMessageHelper.setText(html, true);

            javaMailSender.send(mimeMessage);
            logger.info("## Send the mail with html success ...");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Send html mail error: ", e);
            return false;
        }

        return true;
    }

    /**
     * 发送带有图片的 html 的邮件
     * @param to
     * @param subject
     * @param html
     * @param cids
     * @param filePaths
     * @return
     */
    @Override
    public boolean sendWithImageHtml(String to, String subject, String html, String[] cids, String[] filePaths) {
        logger.info("## Ready to send mail ...");
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = null;
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            // 邮件发送来源
            mimeMessageHelper.setFrom(mailProperties.getUsername());
            // 邮件发送目标
            mimeMessageHelper.setTo(to);
            // 设置标题
            mimeMessageHelper.setSubject(subject);
            // 设置内容,并设置内容 html 格式为 true
            mimeMessageHelper.setText(html, true);

            // 设置 html 中内联的图片
            for (int i = 0; i < cids.length; i++) {
                FileSystemResource file = new FileSystemResource(filePaths[i]);
                // addInline() 方法 cid 需要 html 中的 cid (Content ID) 对应,才能设置图片成功,
                // 具体可以参见,下面 4.3.3 单元测试的参数设置
                mimeMessageHelper.addInline(cids[i], file);
            }

            javaMailSender.send(mimeMessage);
            logger.info("## Send the mail with image success ...");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Send html mail error: ", e);
            return false;
        }

        return true;
    }

    /**
     * 发送带有附件的邮件
     * @param to
     * @param subject
     * @param content
     * @param filePaths
     * @return
     */
    @Override
    public boolean sendWithWithEnclosure(String to, String subject, String content, String[] filePaths) {
        logger.info("## Ready to send mail ...");
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = null;
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            // 邮件发送来源
            mimeMessageHelper.setFrom(mailProperties.getUsername());
            // 邮件发送目标
            mimeMessageHelper.setTo(to);
            // 设置标题
            mimeMessageHelper.setSubject(subject);
            // 设置内容
            mimeMessageHelper.setText(content);

            // 添加附件
            for (int i = 0; i < filePaths.length; i++) {
                FileSystemResource file = new FileSystemResource(filePaths[i]);
                String attachementFileName = "附件" + (i + 1);
                mimeMessageHelper.addAttachment(attachementFileName, file);
            }

            javaMailSender.send(mimeMessage);
            logger.info("## Send the mail with enclosure success ...");
        } catch (Exception e) {
            logger.error("Send html mail error: ", e);
            return false;
        }
        return true;
    }
}

4.2 项目结构

完成上面功能类的编码后,看下项目结构如下:

4.3 单元测试,验证效果

4.3.1 简单文本发送

填写相关测试参数,包括目标邮箱地址,标题,内容,运行单元测试:

单元测试通过,再看下实际效果:

邮件正常发送。

4.3.2 发送 Html

填写相关测试参数,包括目标邮箱地址,标题,html 内容,运行单元测试通过,直接看效果:

可以看到,邮件发送成功!

4.3.3 发送带有图片的 Html

填写相关测试参数,包括目标邮箱地址,标题,html 内容,html 中包含了两张图片,并且 src 中的内容是 cid:{flag}的格式,前缀 cid:是固定的,您需要改变是后面的标志位,通过 addInline(cid, file) 来将 cid 和具体的图片文件对应起来。

运行单元测试通过,看看效果如何:

可以看到 html 中图片也是 OK 的。

PS: 这里笔者在测试发送给 QQ 邮箱的时候,图片显示不成功,暂时还没找到问题在哪,如果有哪位读者知道,不妨后台发个消息告诉一下笔者哈。

4.3.4 发送带有附件的邮件

填写相关测试参数,包括目标邮箱地址,标题,内容,并添加了两个附件,运行单元测试,看看实际效果:

发送成功,到此所有的单元测试全部运行通过。

五、总结

本文中,我们学习如何在 Spring Boot 2.x 版本中集成发送邮件功能,包括发送简单文本,Html 内容,带有图片的 Html 内容,以及带有的附加的邮件,希望对您有所帮助!

六、GitHub 源码地址

https://github.com/weiwosuoai/spring-boot-tutorial/tree/master/spring-boot-mail

赠送 | 面试&学习福利资源

最近在网上发现一个不错的 PDF 资源《Java 核心面试知识.pdf》分享给大家,不光是面试,学习,你都值得拥有!!!

获取方式: 关注公众号: 小哈学Java, 后台回复 资源,既可获取资源链接,下面是目录以及部分截图:

重要的事情说两遍,获取方式: 关注公众号: 小哈学Java, 后台回复 资源,既可获取资源链接 !!!

欢迎关注微信公众号: 小哈学Java

原文地址:https://www.cnblogs.com/quanxiaoha/p/10704635.html

时间: 2024-08-26 23:07:02

Spring Boot 2.0 图文教程 | 集成邮件发送功能的相关文章

Spring Boot 2.0.2 教程 - 目录

Spring Boot 2.0.2 教程 - Hello World - 01 Spring Boot 2.0.2 教程 - Hello World 之 intellij idea 创建web项目 - 02 Spring Boot 2.0.2 教程 - 配置文件application.properties - 03 Spring Boot 2.0.2 教程 - 日志管理 - 04 Spring Boot 2.0.2 教程 - 集成jsp和静态资源css,js,image的访问 - 05 Spri

Spring Boot 2.0 WebFlux 教程 (一) | 入门篇

目录 一.什么是 Spring WebFlux 二.WebFlux 的优势&性能 三.WebFlux 应用场景 四.适用性 五.快速入门 5.1 添加 webflux 依赖 5.2 定义接口 5.3 测试接口 六.总结 七.GitHub 示例代码 一.什么是 Spring WebFlux 下图截自 Spring Boot 官方网站: 结合上图,在了解 Spring WebFlux 之前,我们先来对比说说什么是 Spring MVC,这更有益我们去理解 WebFlux,图右边对 Spring MV

Abp集成邮件发送功能

安装 首先,将Abp.MailKit NuGet包安装到您的项目中: Install-Package Abp.MailKit 添加依赖 [DependsOn(typeof(AbpMailKitModule))] public class MyProjectModule : AbpModule { //... }在创建MailKit的SmtpClient时,您可能需要进行其他配置或自定义.在这种情况下,您可以 使用自己的实现替换 IMailKitSmtpBuilder接口.您可以从DefaultM

Spring Boot 2.0 Intellij Idea 中图文详解打包成可执行Jar

我们使用Spring Boot 2.0 创建好我们的项目后,我们一般需要打包,然后部署到服务器上. 打包步骤: 1. 选中项目,右键--> Open Module Settings. 2. 切换到Artifacts 选项卡下,点击+ 号 3. 点击+ 后,可以看到有很多选项,我们选择Jar, From modules with depedency 4. Main Class 文件浏览或者搜索找到我们的主函数,勾选copy to the out put directory and link via

【SFA官方翻译】使用 Kubernetes、Spring Boot 2.0 和 Docker 的微服务快速指南

[SFA官方翻译]使用 Kubernetes.Spring Boot 2.0 和 Docker 的微服务快速指南 原创: Darren Luo SpringForAll社区 今天 原文链接:https://dzone.com/articles/quick-guide-to-microservices-with-kubernetes-sprin 作者:Piotr Mińkowski 译者:Darren Luo 在本教程中你将学习如何使用 Kubernetes 和 Docker 快速启动并运行 Sp

Spring Boot 2.0(一):【重磅】Spring Boot 2.0权威发布

就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误,然后Spring Boot官方又赶紧把 GitHub 上发布的 v2.0.0.RELEASE 版本进行了撤回.到了下午将问题修复后,又重新进行了上传,至此Spring Boot2.0正式推出! 要知道这是Spring Boot1.0发布4年之后第一次重大修订,因此有多的新功能和特性值得大家期待!在S

阿里P9告诉你 Spring Boot 2.0正式发布,升还是不升呢?

Spring帝国Spring几乎是每一位Java开发人员都耳熟能详的开发框架,不论您是一名初出茅庐的程序员还是经验丰富的老司机,都会对其有一定的了解或使用经验.在现代企业级应用架构中,Spring技术栈几乎成为了Java语言的代名词,那么Spring为什么能够在众多开源框架中脱颖而出,成为业内一致认可的技术解决方案呢?我们不妨从最初的Spring Framework开始,看看它为什么能够横扫千军,一统江湖! 挑战权威,一战成名 2004年3月,Spring的第一个版本以及其创始人Rod John

Spring Boot 2.0(六):使用 Docker 部署 Spring Boot 开源软件云收

只需三步即可部署开源项目云收藏,打造专属个人的收藏系统,就是这么简单! 云收藏项目已经开源2年多了,作为当初刚开始学习 Spring Boot 的练手项目,使用了很多当时很新的技术,现在看来其实很多新技术是没有必要使用的,但做为学习案例来讲确实是一个绝佳的 Spring Boot 实践. 从开源到现在,写了一些教程给大家介绍如何部署云收藏,如何在IDE中运行云收藏,但是仍然有很多的朋友不知道如何使用,如何部署?就像"请提供一份云收藏数据结构" 这样的问题我至少都回答了一百多次,并且在

Spring Boot 2.0干货系列:(一)Spring Boot1.5X升级到2.0指南

前言Spring Boot已经发布2.0有满久了,多了很多新特性,一些坑也慢慢被填上,最近有空,就把本博客中Spring Boot干货系列对应的源码从1.5X升级到Spring Boot 2.0,顺便整理下升级的时候遇到的一些坑,做个记录.后续的教程就以最新的2.03版本为主.依赖 JDK 版本升级 2.x 至少需要 JDK 8 的支持,2.x 里面的许多方法应用了 JDK 8 的许多高级新特性,所以你要升级到 2.0 版本,先确认你的应用必须兼容 JDK 8. 另外,2.x 开始了对 JDK