From adebdff244d2ba30fab306ff433b79376c417bae Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@gmail.com>
Date: 星期三, 12 二月 2025 20:49:04 +0800
Subject: [PATCH] #

---
 rsf-common/src/main/java/com/vincent/rsf/common/utils/Utils.java                      |   12 ++++++++++++
 rsf-server/src/main/java/com/vincent/rsf/server/common/service/EmailService.java      |   33 ++++++++++++++++++++++++++++-----
 rsf-server/src/main/java/com/vincent/rsf/server/system/controller/AuthController.java |    2 +-
 3 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/rsf-common/src/main/java/com/vincent/rsf/common/utils/Utils.java b/rsf-common/src/main/java/com/vincent/rsf/common/utils/Utils.java
index d718c97..06a5b0b 100644
--- a/rsf-common/src/main/java/com/vincent/rsf/common/utils/Utils.java
+++ b/rsf-common/src/main/java/com/vincent/rsf/common/utils/Utils.java
@@ -8,6 +8,7 @@
 import java.util.Optional;
 import java.util.function.BiConsumer;
 import java.util.function.Function;
+import java.util.Map;
 
 /**
  * Created by vincent on 2023/3/14
@@ -292,4 +293,15 @@
         return snakeCaseField + (order.isEmpty() ? "" : " " + order);
     }
 
+    public static String processTemplate(String template, Map<String, Object> params) {
+        if (template == null || params == null) {
+            return template;
+        }
+        String processed = template;
+        for (Map.Entry<String, Object> entry : params.entrySet()) {
+            processed = processed.replace("${" + entry.getKey() + "}", entry.getValue().toString());
+        }
+        return processed;
+    }
+
 }
diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/common/service/EmailService.java b/rsf-server/src/main/java/com/vincent/rsf/server/common/service/EmailService.java
index 6a377d3..3c33cfd 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/common/service/EmailService.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/common/service/EmailService.java
@@ -1,5 +1,8 @@
 package com.vincent.rsf.server.common.service;
 
+import com.alibaba.fastjson.JSON;
+import com.vincent.rsf.common.utils.Utils;
+import com.vincent.rsf.framework.common.Cools;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -10,6 +13,7 @@
 
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
+import java.util.Map;
 
 @Slf4j
 @Service
@@ -18,19 +22,38 @@
     @Value("${spring.mail.username}")
     private String from;
 
+    private final static String TEMPLATE = "Your verification code is: ${code}";
+
     @Autowired
+    @SuppressWarnings("all")
     private JavaMailSender mailSender;
 
-    public boolean sendVerificationEmail(String to, String verificationCode) {
+    public boolean sendEmail(String to, String subject, Map<String, Object> param) {
+
+        String content = "Your verification code is: ${code}";
+        if (!Cools.isEmpty(param) && !param.isEmpty()) {
+            content = Utils.processTemplate(content, param);
+        }
+
+        boolean sendRes = this.send(this.from, to, subject, content);
+        if (sendRes) {
+            log.info("Email sent successfully. To: {}, Subject: {}, Parameter: {}"
+                    , to
+                    , subject
+                    , JSON.toJSONString(param));
+        }
+        return sendRes;
+    }
+
+    public boolean send(String from, String to, String subject, String content) {
         try {
             MimeMessage message = mailSender.createMimeMessage();
             message.setFrom(from);
-            MimeMessageHelper helper = new MimeMessageHelper(message, true);
 
-            // 閭欢涓婚鍜屽唴瀹�
+            MimeMessageHelper helper = new MimeMessageHelper(message, true);
             helper.setTo(to);
-            helper.setSubject("Email Verification Code");
-            helper.setText("Your verification code is: " + verificationCode);
+            helper.setSubject(subject);
+            helper.setText(content);
 
             mailSender.send(message);
             return true;
diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/system/controller/AuthController.java b/rsf-server/src/main/java/com/vincent/rsf/server/system/controller/AuthController.java
index 01afd24..7149435 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/system/controller/AuthController.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/system/controller/AuthController.java
@@ -58,7 +58,7 @@
     @PostMapping("/login")
     public R login(@RequestBody LoginParam param, HttpServletRequest request) {
 //        System.out.println(userService.encodePassword("123456"));
-//        emailService.sendVerificationEmail("t1341870251@gmail.com", "123456");
+        emailService.sendEmail("t1341870251@gmail.com", "Email Verification Code", Cools.add("code", "666"));
 
         String username = param.getUsername();
         Long tenantId = param.getTenantId();

--
Gitblit v1.9.1