首页 > 其他分享 >SpringBoot+mail 轻松实现各类邮件自动推送

SpringBoot+mail 轻松实现各类邮件自动推送

时间:2024-10-16 14:22:01浏览次数:1  
标签:SpringBoot smtp new 发送 props mail 推送 邮件

在实际的项目开发过程中,经常需要用到邮件通知功能。例如,通过邮箱注册,邮箱找回密码,邮箱推送报表等等,实际的应用场景非常的多。

早期的时候,为了能实现邮件的自动发送功能,通常会使用 JavaMail 相关的 api 来完成。后来 Spring 推出的 JavaMailSender 工具,进一步简化了邮件的自动发送过程,调用其 send 方法即可发送邮件。再之后, Spring Boot 针对邮件推送功能推出了spring-boot-starter-mail工具包,开发者可以通过它来快速实现邮件发送服务。

今天通过这篇文章,我们一起来学习如何在 Spring Boot 中快速实现一个自动发送邮件的功能。

01、环境准备

在介绍邮件推送实现之前,我们需要先准备一台邮件推送的服务器,以便实现相关功能。

这里以腾讯邮箱为例,将其作为邮件发送的中转平台。

1.1、开启 SMTP 服务

登陆腾讯邮箱,打开【设置】-》【收发信设置】,开启 SMTP 服务,最后点击【保存更改】。

图片

1.2、生成客户端专用密码

点击【设置】-》【账户】,进入页面后点击【开启安全登陆】,点击【生成新密码】。

图片图片图片

这个新密码会用于邮箱的自动发送,因此需要记录下来,最后点击【保存更改】。

1.3、相关扩展知识

  • 什么是 SMTP?

SMTP(simple mail transfer protocol),也被称为简单邮件传输协议,主要用于发送电子邮件的,通过它可以实现邮件的发送或者中转。遵循 SMTP 协议的服务器,通常称为发送邮件服务器。

  • 什么是 POP3?

POP3(Post Office Protocol),一种邮局通信协议。主要用于接受电子邮件的,POP3 允许用户从服务器上把邮件存储到自己的计算机上,同时删除保存在邮件服务器上的邮件。同理,遵循 POP3 协议的服务器,通常称为接收邮件服务器。

  • 什么是 IMAP?

IMAP(Internet Mail Access Protocol),一种交互式邮件存取协议。与 POP3 协议类似,主要用于接收电子邮件,稍有不同的是:IMAP 允许电子邮件客户端收取的邮件仍然保留在服务器上,同时在客户端上的操作都会反馈到服务器上,例如删除邮件,标记已读等,服务器上的邮件也会做相应的动作。所以无论从浏览器登录邮箱或者客户端软件登录邮箱,看到的邮件以及状态都是一致的。

总结下来就是:SMTP 负责发送邮件,POP3/IMAP 负责接收邮件。

常见邮箱发、收服务器如下!

图片

02、邮件推送实现

用于发送邮件的服务器、账户和密码准备好了之后,就可以正式使用了。下面我们以 Spring Boot 的 2.1.0版本为基础,实现过程如下。

2.1、添加依赖包

pom.xml文件中,添加spring-boot-starter-mail依赖包。

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

2.2、添加相关配置

application.properties中添加邮箱相关配置。

# 配置邮件发送主机地址
spring.mail.host=smtp.exmail.qq.com
# 配置邮件发送服务端口号
spring.mail.port=465
# 配置邮件发送服务协议
spring.mail.protocol=smtp
# 配置邮件发送者用户名或者账户
[email protected]
# 配置邮件发送者密码或者授权码
spring.mail.password=xxxxxxx
# 配置邮件默认编码
spring.mail.default-encoding=UTF-8
# 配置smtp相关属性
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.ssl.required=true

2.3、简单发送一封邮件

通过单元测试来实现一封简单邮件的发送,示例如下:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailSimpleTest {

    @Autowired
    private JavaMailSender mailSender;

    @Test
    public void sendSimpleMail() throws Exception {
        SimpleMailMessage message = new SimpleMailMessage();
        // 配置发送者邮箱
        message.setFrom("[email protected]");
        // 配置接受者邮箱
        message.setTo("[email protected]");
        // 配置邮件主题
        message.setSubject("主题:简单邮件");
        // 配置邮件内容
        message.setText("测试邮件内容");
        // 发送邮件
        mailSender.send(message);
    }
}

运行单元测试之后,如果不出意外的话,接受者会收到这样的一封邮件。

图片

至此,邮件发送成功!

2.4、发送 HTML 格式邮件

在实际的业务开发中,邮件的内容通常会要求丰富,比如会发送一些带有图片的内容,包括字体大小,各种超链接等,这个时候如何实现呢?

实际上,邮件内容支持 HTML 格式,因此可以借助页面模板引擎来实现绚丽多彩的内容。

下面我们以freemarker模板引擎为例,发送一封内容为 HTML 格式的邮件。

2.4.1、引入 freemarker 依赖包

首先,在pom.xml文件中,添加freemarker依赖包。

<!--freemarker 支持-->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
</dependency>
2.4.2、编写邮件页面模板

然后,在resources/templates目录下,创建一个demo.ftl文件,示例如下!

<html>
<head>
 <meta charset="utf-8">
 <title></title>
</head>
<body>
<div>您好:${userName}</div>
<div>这是html文本内容</div>
<img src="https://rescdn.qqmail.com/zh_CN/htmledition/images/logo/[email protected]" />
</body>
</html>
2.4.3、编写一个邮件推送服务

虽然采用 Spring Boot 提供的自动配置属性来实现邮件推送,可以极大的简化开发过程。而实际开发的时候,通常更推荐自定义一个邮件统一推送服务,这样更便于灵活的控制代码实现以及排查相关问题。

邮件统一发送服务,示范如下。

@Component
public class MailPushService {

    private final Logger LOGGER = LoggerFactory.getLogger(MailPushService.class);

    @Value("${mail.host}")
    private String host;

    @Value("${mail.port}")
    private String port;

    @Value("${mail.protocol}")
    private String protocol;

    @Value("${mail.username}")
    private String username;

    @Value("${mail.password}")
    private String password;

    @Value("${mail.fromEmail}")
    private String fromEmail;

    @Value("${mail.fromPersonal}")
    private String fromPersonal;

    @Autowired
    private JavaMailSender mailSender;


    /**
     * 发送邮件(简单模式)
     * @param toEmail
     * @param subject
     * @param content
     */
    public void sendMail(String toEmail, String subject,String content)  {
        try {
            final Properties props = new Properties();
            //服务器
            props.put("mail.smtp.host", host);
            //端口
            props.put("mail.smtp.port", port);
            //协议
            props.setProperty("mail.transport.protocol", protocol);
            //用户名
            props.put("mail.user", username);
            //密码
            props.put("mail.password", password);
            //使用smtp身份验证
            props.put("mail.smtp.auth", "true");

            //开启安全协议
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", sf);
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(props.getProperty("mail.user"),
                            props.getProperty("mail.password"));
                }
            };

            Session session = Session.getDefaultInstance(props, authenticator);
            session.setDebug(true);
            MimeMessage mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(new InternetAddress(fromEmail, MimeUtility.encodeText(fromPersonal)));
            mimeMessage.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toEmail));
            mimeMessage.setSubject(subject);
            mimeMessage.setContent(content, "text/html;charset=UTF-8");

            //保存信息
            mimeMessage.saveChanges();
            //发送消息
            Transport.send(mimeMessage);
            LOGGER.info("简单邮件已经发送。");
        } catch (Exception e) {
            LOGGER.error("发送简单邮件时发生异常!", e);
        }
    }
}

代码中相关自定义的全局参数配置如下:

mail.host=smtp.exmail.qq.com
mail.port=465
mail.protocol=smtp
[email protected]
mail.password=xxxxxx
[email protected]
mail.fromPersonal=发送者昵称
2.4.4、测试服务的正确性

最后,编写一个单元测试来验证服务的正确性,示例如下:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {

    @Autowired
    private MailPushService mailPushService;

    @Test
    public void testSendHtmlMail() throws Exception {
        String sendHtml = buildHtmlContent("张三");
        mailPushService.sendMail("[email protected]","简单标题", sendHtml);
    }

    /**
     * 封装html页面
     * @return
     * @throws Exception
     */
    private static String buildHtmlContent(String userName) throws Exception {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
        configuration.setDefaultEncoding(Charset.forName("UTF-8").name());
        configuration.setClassForTemplateLoading(MailTest.class, "/templates");
        // 获取页面模版
        Template template = configuration.getTemplate("demo.ftl");
        // 动态变量替换
        Map<String,Object> map = new HashMap<>();
        map.put("userName", userName);
        String htmlStr = FreeMarkerTemplateUtils.processTemplateIntoString(template,map);
        return htmlStr;
    }

}

运行单元测试之后,如果没有报错,接受者会收到这样的一封邮件。

图片

2.5、发送带附件的邮件

某些业务场景,用户希望发送的邮件中能带上附件,比如上文中,在发送 HTML 格式的邮件时,同时也带上文件附件,这个时候如何实现呢?

2.5.1、编写带附件的邮件发送

此时可以在邮件推送服务中,新增一个支持带附件的方法,实现逻辑如下。

/**
 * 发送邮件(复杂模式)
 * @param toEmail    接受者邮箱
 * @param subject    主题
 * @param sendHtml   内容
 * @param attachment 附件
 */
public void sendMail(String toEmail, String subject, String sendHtml, File attachment) {
    try {
        //设置了附件名过长问题
        System.setProperty("mail.mime.splitlongparameters", "false");
        final Properties props = new Properties();
        //服务器
        props.put("mail.smtp.host", host);
        //端口
        props.put("mail.smtp.port", port);
        //协议
        props.setProperty("mail.transport.protocol", protocol);
        //用户名
        props.put("mail.user", username);
        //密码
        props.put("mail.password", password);
        //使用smtp身份验证
        props.put("mail.smtp.auth", "true");

        //开启安全协议
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(props.getProperty("mail.user"),
                        props.getProperty("mail.password"));
            }
        };

        Session session = Session.getDefaultInstance(props, authenticator);
        session.setDebug(true);
        MimeMessage mimeMessage = new MimeMessage(session);
        // 发送者邮箱
        mimeMessage.setFrom(new InternetAddress(fromEmail, MimeUtility.encodeText(fromPersonal)));
        // 接受者邮箱
        mimeMessage.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toEmail));
        // 邮件主题
        mimeMessage.setSubject(subject);
        // 定义邮件内容
        Multipart multipart = new MimeMultipart();

        // 添加邮件正文
        BodyPart contentPart = new MimeBodyPart();
        contentPart.setContent(sendHtml, "text/html;charset=UTF-8");
        multipart.addBodyPart(contentPart);

        // 添加附件
        if (attachment != null) {
            BodyPart attachmentBodyPart = new MimeBodyPart();
            // MimeUtility.encodeWord可以避免文件名乱码
            FileDataSource fds=new FileDataSource(attachment);
            attachmentBodyPart.setDataHandler(new DataHandler(fds));
            attachmentBodyPart.setFileName(MimeUtility.encodeText(fds.getName()));
            multipart.addBodyPart(attachmentBodyPart);
        }

        // 将multipart对象放到message中
        mimeMessage.setContent(multipart);

        //保存信息
        mimeMessage.saveChanges();
        //发送消息
        Transport.send(mimeMessage);
        LOGGER.info("邮件已经发送。");
    } catch (Exception e) {
        LOGGER.error("发送邮件时发生异常!", e);
    }
}
2.5.2、测试服务的正确性

最后,编写一个单元测试来验证服务的正确性,示例如下:

@Test
public void doSendHtmlEmail() throws Exception {
    // 获取正文内容
    String sendHtml = buildHtmlContent("张三");

    // 获取附件
    File file = new File( "~/doc/Java开发手册.pdf");
    // 发送邮件
    mailPushService.sendMail("[email protected]","带附件的邮件推送", sendHtml, file);
}

运行单元测试之后,如果没有报错,接受者会收到这样的一封邮件。

图片

03、小结

最后总结一下,邮件自动推送功能在实际的业务系统中应用非常广,在发送过程中也可能会因为网络问题出现各种失败现象,因此推荐采用异步的方式来发送邮件,例如采用异步编程或者消息队列来实现,以便加快主流程的执行速度。

想要获取项目源代码的小伙伴,可以访问如下地址。

https://gitee.com/pzblogs/spring-boot-example-demo

04、参考

1.https://blog.csdn.net/qq_26383975/article/details/121957917

1.http://www.ityouknow.com/springboot/2017/05/06/spring-boot-mail.html

标签:SpringBoot,smtp,new,发送,props,mail,推送,邮件
From: https://www.cnblogs.com/qxqbk/p/18469872

相关文章

  • 基于SpringBoot+Vue的校园周边美食探索及分享平台的设计与实现(带文档)
    基于SpringBoot+Vue的校园周边美食探索及分享平台的设计与实现(带文档)开发语言:Java数据库:MySQL技术:SpringBoot+MyBatis+Vue等工具:IDEA/Ecilpse、Navicat、Maven源码校园周边美食探索及分享平台是一个旨在为校园用户提供便捷的美食发现和分享服务的系统。该平台通过现......
  • 支付宝沙箱版(什么是支付宝沙箱、配置支付宝沙箱、配置内网穿透、在SpringBoot项目中对
    文章目录0.前言1.什么是支付宝沙箱2.配置支付宝沙箱2.1沙箱应用的应用信息(获取app-id和gateway-url)2.2沙箱账号的商家信息和买家信息2.3下载秘钥工具2.4生成秘钥(获取private-key)2.5配置秘钥(获取alipay-public-key)3.配置内网穿透3.1使用cpolar实现内网穿透3.2......
  • java毕业设计-基于Springboot的社区医疗服务可视化系统【d代码+论文+PPT】
    全文内容包括:1、采用技术;2、系统功能;3、系统截图;4、部分代码;5、配套内容。索取方式见文末微信号,欢迎关注收藏!一、采用技术语言:Java1.8框架:Springboot数据库:MySQL5.7、8.0开发工具:IntelliJIDEA旗舰版其他:Maven3.8以上二、系统功能管理员管理:负责系统后台维护,确保运行......
  • jsp电影推送及电影数据管理系统3f6db(程序+源码+数据库+调试部署+开发环境)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容一、课题背景与意义随着电影产业的快速发展,用户对电影的选择需求日益多样化。然而,如何从海量的电影资源中精准地找到用户感兴趣的内容,成为了一个亟......
  • springboot超市商品管理系统-计算机毕业设计源码55289
    摘 要随着信息技术的快速发展和普及,传统的超市管理模式已经无法满足现代商业的需求。为了提高超市的管理效率,优化商品销售流程,本文提出了一种基于SpringBoot框架的超市商品管理系统。该系统结合了现代软件开发技术,包括MySQL数据库、Java语言等,实现了对超市商品的全面管理。......
  • springboot社区团购系统-计算机毕业设计源码50782
    摘要随着互联网技术的不断发展和普及,社区团购作为一种新型的互联网零售模式,已经成为了人们越来越喜欢的购物方式之一。社区团购系统是支撑社区团购业务的重要基础设施,其功能和性能的好坏直接影响到社区团购业务的发展和用户体验。本论文研究了社区团购系统的需求分析、系统实......
  • springboot校园运动会管理系统-计算机毕业设计源码94492
    摘要校园运动会作为学校重要的体育活动之一,对于促进学生身心健康、增强团队合作意识具有重要意义。为了更好地组织和管理校园运动会,开发了基于SpringBoot的校园运动会管理系统。该系统旨在整合现代信息技术,提高运动会的组织效率和参与体验。通过该系统,学校可以方便地进行运......
  • python+eel+ws实现消息推送
    ws服务器是单独的,专门用来推送消息。js用来连接ws,发消息。eel程序用户处理消息ws服务器importwebsocketsimportasyncio#存储所有WebSocket连接的集合connected_clients=set()asyncdefwebsocket_handler(websocket,path):#将新的连接添加到集合中connec......
  • 体检预约毕业设计社区体检管理网站预约排号基于SpringBootSSM框架
    目录1、项目概述‌1.1开发背景‌2、技术选择2.1IDEA开发工具2.2SpringBoot框架‌3需求分析3.1功能模块设计‌3.2非功能需求‌‌4、数据库设计‌‌5、界面设计‌‌6、安全性设计‌1、项目概述‌随着社区居民健康意识的不断提升,体检服务的需求也日益增长。然......
  • springboot基于java的汽车票网上预订系统(源码+java+vue+部署文档+讲解等)
    收藏关注不迷路!!......