feat:
- 添加Email测试类
This commit is contained in:
parent
acf4c56b2b
commit
6f7057ba06
5
pom.xml
5
pom.xml
@ -28,7 +28,6 @@
|
||||
</scm>
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<mail.version>1.6.2</mail.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@ -59,8 +58,8 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<version>${mail.version}</version>
|
||||
<artifactId>jakarta.mail</artifactId>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package asia.yulinling.workflow.service;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import jakarta.mail.MessagingException;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -46,14 +46,15 @@ public interface MailService {
|
||||
void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException;
|
||||
|
||||
/**
|
||||
* 发送带附件邮件
|
||||
* 发送正文带静态资源的邮件
|
||||
*
|
||||
* @param to 收件人地址
|
||||
* @param subject 邮件主题
|
||||
* @param content 邮件内容
|
||||
* @param rscPath 静态资源地址
|
||||
* @param rscId 静态资源id
|
||||
* @param cc 抄送地址
|
||||
* @throws MessagingException 邮件发送异常
|
||||
*/
|
||||
void sendResourceMail(String to, String subject, String content, String rscPath, String... cc) throws MessagingException;
|
||||
void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException;
|
||||
}
|
||||
|
||||
@ -4,11 +4,15 @@ import asia.yulinling.workflow.service.MailService;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
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 javax.mail.MessagingException;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import jakarta.mail.MessagingException;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -36,6 +40,7 @@ public class MailServiceImpl implements MailService {
|
||||
@Override
|
||||
public void sendSimpleMail(String to, String subject, String content, String... cc) {
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
|
||||
message.setFrom(from);
|
||||
message.setTo(to);
|
||||
message.setSubject(subject);
|
||||
@ -43,6 +48,7 @@ public class MailServiceImpl implements MailService {
|
||||
if (ArrayUtil.isNotEmpty(cc)) {
|
||||
message.setCc(cc);
|
||||
}
|
||||
|
||||
mailSender.send(message);
|
||||
}
|
||||
|
||||
@ -57,7 +63,18 @@ public class MailServiceImpl implements MailService {
|
||||
*/
|
||||
@Override
|
||||
public void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException {
|
||||
MimeMessage message = mailSender.createMimeMessage();
|
||||
|
||||
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
||||
helper.setFrom(from);
|
||||
helper.setTo(to);
|
||||
helper.setSubject(subject);
|
||||
helper.setText(content, true);
|
||||
if (ArrayUtil.isNotEmpty(cc)) {
|
||||
helper.setCc(cc);
|
||||
}
|
||||
|
||||
mailSender.send(message);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -72,21 +89,47 @@ public class MailServiceImpl implements MailService {
|
||||
*/
|
||||
@Override
|
||||
public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException {
|
||||
MimeMessage message = mailSender.createMimeMessage();
|
||||
|
||||
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
||||
helper.setFrom(from);
|
||||
helper.setTo(to);
|
||||
helper.setSubject(subject);
|
||||
helper.setText(content, true);
|
||||
if (ArrayUtil.isNotEmpty(cc)) {
|
||||
helper.setCc(cc);
|
||||
}
|
||||
FileSystemResource file = new FileSystemResource(new File(filePath));
|
||||
helper.addAttachment(file.getFilename(), file);
|
||||
|
||||
mailSender.send(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送带附件邮件
|
||||
* 发送正文带静态资源的邮件
|
||||
*
|
||||
* @param to 收件人地址
|
||||
* @param subject 邮件主题
|
||||
* @param content 邮件内容
|
||||
* @param rscPath 静态资源地址
|
||||
* @param rscId 静态资源id
|
||||
* @param cc 抄送地址
|
||||
* @throws MessagingException 邮件发送异常
|
||||
*/
|
||||
@Override
|
||||
public void sendResourceMail(String to, String subject, String content, String rscPath, String... cc) throws MessagingException {
|
||||
public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException {
|
||||
MimeMessage message = mailSender.createMimeMessage();
|
||||
|
||||
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
||||
helper.setFrom(from);
|
||||
helper.setTo(to);
|
||||
helper.setSubject(subject);
|
||||
helper.setText(content, true);
|
||||
if (ArrayUtil.isNotEmpty(cc)) {
|
||||
helper.setCc(cc);
|
||||
}
|
||||
FileSystemResource file = new FileSystemResource(new File(rscPath));
|
||||
helper.addAttachment(rscId, file);
|
||||
|
||||
mailSender.send(message);
|
||||
}
|
||||
}
|
||||
|
||||
22
src/main/resources/email/test.html
Normal file
22
src/main/resources/email/test.html
Normal file
@ -0,0 +1,22 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Test Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="welcome">
|
||||
<h3>
|
||||
欢迎使用 TEST HTML
|
||||
<span th:text="${project}">- Powered By <span th:text="${author}"></span></span>
|
||||
</h3>
|
||||
|
||||
<span style="text-align: center; padding: 10px">
|
||||
<a style="text-decoration: none" href="#" th:href="@{${url}}" target="_blank">
|
||||
<strong>Spring Boot入门首选</strong>
|
||||
</a>
|
||||
</span>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
BIN
src/main/resources/static/GitHub.ico
Normal file
BIN
src/main/resources/static/GitHub.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 170 KiB |
22
src/main/resources/templates/welcome.html
Normal file
22
src/main/resources/templates/welcome.html
Normal file
@ -0,0 +1,22 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="welcome">
|
||||
<h3>
|
||||
欢迎使用
|
||||
<span th:text="${project}">- Powered By <span th:text="${author}"></span></span>
|
||||
</h3>
|
||||
|
||||
<span style="text-align: center; padding: 10px">
|
||||
<a style="text-decoration: none" href="#" th:href="@{${url}}" target="_blank">
|
||||
<strong>Spring Boot入门首选</strong>
|
||||
</a>
|
||||
</span>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@ -0,0 +1,101 @@
|
||||
package asia.yulinling.workflow.service;
|
||||
|
||||
import asia.yulinling.workflow.WorkFlowMainTests;
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import jakarta.mail.MessagingException;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.thymeleaf.context.Context;
|
||||
import org.thymeleaf.TemplateEngine;
|
||||
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 邮件测试
|
||||
* </p>
|
||||
*
|
||||
* @author yulinling
|
||||
* @date 2025/6/4
|
||||
*/
|
||||
public class EmailServiceTest extends WorkFlowMainTests {
|
||||
@Autowired
|
||||
private MailService mailService;
|
||||
@Autowired
|
||||
private TemplateEngine templateEngine;
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
/**
|
||||
* 测试简单邮件
|
||||
*/
|
||||
@Test
|
||||
public void sendSimpleMail() {
|
||||
mailService.sendSimpleMail("rsgltzyd@hotmail.com", "test", "test content");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试发送HTML邮件
|
||||
*
|
||||
* @throws MessagingException 邮件异常
|
||||
*/
|
||||
@Test
|
||||
public void sendHtmlMail() throws MessagingException {
|
||||
Context context = new Context();
|
||||
context.setVariable("project", "mail test");
|
||||
context.setVariable("author", "yulinlng");
|
||||
context.setVariable("url", "https://github.com/xkcoding/spring-boot-demo");
|
||||
|
||||
String emailTemplate = templateEngine.process("welcome", context);
|
||||
|
||||
mailService.sendHtmlMail("rsgltzyd@hotmail.com", "test", emailTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试自定义模板HTML邮件
|
||||
*
|
||||
* @throws MessagingException 邮件异常
|
||||
*/
|
||||
@Test
|
||||
public void sendHtmlMail2() throws MessagingException {
|
||||
|
||||
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
|
||||
resolver.setApplicationContext(applicationContext);
|
||||
resolver.setCacheable(false);
|
||||
resolver.setPrefix("classpath:/email/");
|
||||
resolver.setSuffix(".html");
|
||||
|
||||
templateEngine.setTemplateResolver(resolver);
|
||||
|
||||
Context context = new Context();
|
||||
context.setVariable("project", "mail test");
|
||||
context.setVariable("author", "yulinlng");
|
||||
context.setVariable("url", "https://github.com/xkcoding/spring-boot-demo");
|
||||
|
||||
String emailTemplate = templateEngine.process("test", context);
|
||||
|
||||
mailService.sendHtmlMail("rsgltzyd@hotmail.com", "test", emailTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试附件邮件
|
||||
*
|
||||
* @throws MessagingException
|
||||
*/
|
||||
@Test
|
||||
public void sendAttachmentsMail() throws MessagingException {
|
||||
URL resource = ResourceUtil.getResource("static/GitHub.ico");
|
||||
mailService.sendAttachmentsMail("rsgltzyd@hotmail.com", "test", "邮件中有附件,请注意查收", resource.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendResourceMail() throws MessagingException {
|
||||
String rscId = "rscId";
|
||||
String content = "<html><body><img src=\"cid:rscId\"></body></html>";
|
||||
URL resource = ResourceUtil.getResource("static/GitHub.ico");
|
||||
|
||||
mailService.sendResourceMail("rsgltzyd@hotmail.com", "test", content, resource.getPath(), rscId);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user