NipGeihou's blog NipGeihou's blog
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档

NipGeihou

我见青山多妩媚,料青山见我应如是
  • Java

    • 开发规范
    • 进阶笔记
    • 微服务
    • 快速开始
    • 设计模式
  • 其他

    • Golang
    • Python
    • Drat
  • Redis
  • MongoDB
  • 数据结构与算法
  • 计算机网络
  • 应用

    • Grafana
    • Prometheus
  • 容器与编排

    • KubeSphere
    • Kubernetes
    • Docker Compose
    • Docker
  • 组网

    • TailScale
    • WireGuard
  • 密码生成器
  • 英文单词生成器
🍳烹饪
🧑‍💻关于
  • 分类
  • 标签
  • 归档
  • 设计模式

  • 开发规范

  • 经验分享

  • 记录

  • 快速开始

    • Spring Boot整合RabbitMQ
    • Spring Boot整合Email邮件发送
      • spring-boot-starter-mail
        • pom.xml
        • application.yml
        • EmailUtilTest
        • 参考
      • Freemarker(HTML邮件)
        • pom.xml
        • Templates
        • MyTemplateConfig.java
        • Test
        • 参考
    • Spring Boot整合jasypt加密配置文件
    • Spring Boot整合单元测试
    • Spring Boot整合优雅关机
    • Spring Boot整合Redis分布式锁
    • Spring Boot整合MyBatis-plus
    • XXL-JOB快速上手
    • Spring Boot整合WebSocket(stomp协议)
    • SpringBoot整合i18n(多语言)
    • 第三方登录 - Google
    • 第三方登录 - Facebook
    • Spring Boot 整合Elasticsearch
  • 笔记

  • 面试题

  • 微服务

  • 踩过的坑

  • Java
  • 快速开始
NipGeihou
2022-01-14
目录

Spring Boot整合Email邮件发送

除此,hutool 的邮件工具 - MailUtil (opens new window) 也是一个不错的选择,甚至使用上更简单

# spring-boot-starter-mail

# pom.xml

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

最新版本在此处 (opens new window)查看

# application.yml

spring:
  mail:
    host: smtp.qq.com
    username: [email protected]
    password: xxxxxxxx
    port: 465
#    Additional JavaMail Session properties:https://javaee.github.io/javamail/docs/api/com/sun/mail/smt
    properties:
      mail.smtp.auth: true
      mail.smtp.starttls.enable: true
      mail.smtp.starttls.required: true
      mail.smtp.ssl.enable: true

# EmailUtilTest

发送可携带附件、HTML 文本的邮件

package com.xxx.system;

import com.github.rjeschke.txtmark.Processor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @author NipGeihou
 * @create 2022-01-13 23:09
 */
@SpringBootTest
public class EmailUtilTest {

    @Autowired
    private JavaMailSender emailSender;

    @Test
    public void test() throws MessagingException {
        String code = String.valueOf(RandomUtil.randomInt(100000, 999999));
        String markdown_content = String.format("您的邮箱验证为:**%s**,有效期为10分钟,请确保本人操作,以免带来不必要损失,感谢您的使用。", code);

        MimeMessage message = emailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom("Pcoder Service<[email protected]>");
        helper.setTo("[email protected]");
        helper.setSubject("测试");
        helper.setText(Processor.process(markdown_content), true);
        emailSender.send(message);
    }
}

# 参考

  • Guide to Spring Email | Baeldung (opens new window)
  • com.sun.mail.imap (JavaMail API documentation) (opens new window)
  • com.sun.mail.pop3 (JavaMail API documentation) (opens new window)
  • com.sun.mail.smtp (JavaMail API documentation) (opens new window)

# Freemarker(HTML 邮件)

很多时候,为了让接收方有更好的体验,我们通常发送 HTML 邮件,因此需要一个 HTML 模板引擎来方便操作。

# pom.xml

<!-- HTML模板 -->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker-gae</artifactId>
    <version>2.3.31</version>
</dependency>

# Templates

src/main/resources/mail-templates/code.ftl
点击查看
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<div>Hi,${name}</div>
</body>
</html>

# MyTemplateConfig.java

package cn.nipx.system.config;

import cn.hutool.extra.template.TemplateConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author NipGeihou
 * @create 2022-01-18 14:47
 */
@Configuration
public class MyTemplateConfig {

    @Bean
    public TemplateConfig mailTemplatesConfig() {
        return new TemplateConfig("mail-templates", TemplateConfig.ResourceMode.CLASSPATH);
    }
}

# Test

package cn.nipx.system;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @author NipGeihou
 * @create 2022-01-13 23:09
 */
@SpringBootTest
public class EmailServiceTest {

    @Autowired
    private TemplateConfig mailTemplatesConfig;


    /**
     * 整合模板发送
     */
    @Test
    public void test() {
        String code = String.valueOf(RandomUtil.randomInt(100000, 999999));
        TemplateEngine engine = TemplateUtil.createEngine(mailTemplatesConfig);
        Template template = engine.getTemplate("code.ftl");
        String html = template.render(Dict.create().set("code", code));
        MailUtil.send("[email protected]", "测试", html, true);
    }


}

# 参考

  • 模板引擎封装 - TemplateUtil (opens new window)
上次更新: 2022/12/31, 03:04:26
Spring Boot整合RabbitMQ
Spring Boot整合jasypt加密配置文件

← Spring Boot整合RabbitMQ Spring Boot整合jasypt加密配置文件→

最近更新
01
元器件
05-23
02
iSCSI服务搭建
05-10
03
磁盘管理与文件系统
05-02
更多文章>
Theme by Vdoing | Copyright © 2018-2025 NipGeihou | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式