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);
}
}
# 参考
上次更新: 2022/12/31, 03:04:26