diff --git a/pom.xml b/pom.xml index 23331c7..a716a96 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,6 @@ 17 - 1.6.2 @@ -59,8 +58,8 @@ com.sun.mail - javax.mail - ${mail.version} + jakarta.mail + 2.0.1 org.springframework.boot diff --git a/src/main/java/asia/yulinling/workflow/service/MailService.java b/src/main/java/asia/yulinling/workflow/service/MailService.java index 57f8cee..337cc9b 100644 --- a/src/main/java/asia/yulinling/workflow/service/MailService.java +++ b/src/main/java/asia/yulinling/workflow/service/MailService.java @@ -1,6 +1,6 @@ package asia.yulinling.workflow.service; -import javax.mail.MessagingException; +import jakarta.mail.MessagingException; /** *

@@ -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; } diff --git a/src/main/java/asia/yulinling/workflow/service/impl/MailServiceImpl.java b/src/main/java/asia/yulinling/workflow/service/impl/MailServiceImpl.java index 5a3046b..4e34dfa 100644 --- a/src/main/java/asia/yulinling/workflow/service/impl/MailServiceImpl.java +++ b/src/main/java/asia/yulinling/workflow/service/impl/MailServiceImpl.java @@ -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; /** *

@@ -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); } } diff --git a/src/main/resources/email/test.html b/src/main/resources/email/test.html new file mode 100644 index 0000000..6d3140b --- /dev/null +++ b/src/main/resources/email/test.html @@ -0,0 +1,22 @@ + + + + + Test Title + + +

+

+ 欢迎使用 TEST HTML + - Powered By +

+ + + + Spring Boot入门首选 + + + +
+ + \ No newline at end of file diff --git a/src/main/resources/static/GitHub.ico b/src/main/resources/static/GitHub.ico new file mode 100644 index 0000000..f220ff0 Binary files /dev/null and b/src/main/resources/static/GitHub.ico differ diff --git a/src/main/resources/templates/welcome.html b/src/main/resources/templates/welcome.html new file mode 100644 index 0000000..8cb8bd9 --- /dev/null +++ b/src/main/resources/templates/welcome.html @@ -0,0 +1,22 @@ + + + + + Title + + +
+

+ 欢迎使用 + - Powered By +

+ + + + Spring Boot入门首选 + + + +
+ + \ No newline at end of file diff --git a/src/test/java/asia/yulinling/workflow/service/EmailServiceTest.java b/src/test/java/asia/yulinling/workflow/service/EmailServiceTest.java new file mode 100644 index 0000000..01d5e00 --- /dev/null +++ b/src/test/java/asia/yulinling/workflow/service/EmailServiceTest.java @@ -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; + +/** + *

+ * 邮件测试 + *

+ * + * @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 = ""; + URL resource = ResourceUtil.getResource("static/GitHub.ico"); + + mailService.sendResourceMail("rsgltzyd@hotmail.com", "test", content, resource.getPath(), rscId); + } +}