Explorar o código

将邮件配置写入yml文件中

husj hai 1 mes
pai
achega
785d2aeaea

+ 24 - 0
src/main/java/top/husj/husj_wx/properties/MailProperties.java

@@ -0,0 +1,24 @@
+package top.husj.husj_wx.properties;
+
+import lombok.Data;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+@Data
+@Component
+public class MailProperties {
+    @Value("${mail.host}")
+    private String host;
+
+    @Value("${mail.port}")
+    private String port;
+
+    @Value("${mail.username}")
+    private String username;
+
+    @Value("${mail.password}")
+    private String password;
+
+    @Value("${mail.alert-to}")
+    private String alertTo;
+}

+ 10 - 6
src/main/java/top/husj/husj_wx/service/AccessTokenService.java

@@ -6,6 +6,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
+import top.husj.husj_wx.properties.MailProperties;
 import top.husj.husj_wx.properties.WeChatProperties;
 import top.husj.husj_wx.utils.AccessTokenUtil;
 import top.husj.husj_wx.utils.EmailUtil;
@@ -17,6 +18,8 @@ public class AccessTokenService {
     private RestTemplate restTemplate;
     @Autowired
     private WeChatProperties weChatProperties;
+    @Autowired
+    private MailProperties mailProperties;
 
     /**
      * 刷新access_token的核心逻辑
@@ -43,12 +46,13 @@ public class AccessTokenService {
 
         } catch (Exception e) {
             log.error("获取access_token时发生异常: ", e);
-            String host = "smtp.030208.xyz";
-            String port = "465";
-            String username = "husj@030208.xyz";
-            String password = "15629747218hsjH";
-            EmailUtil emailUtil = new EmailUtil(host, port, username, password);
-            emailUtil.sendTextEmail("807244836@qq.com", "微信公众号token获取失败请检查", "微信公众号token获取失败请检查");
+            EmailUtil emailUtil = new EmailUtil(
+                    mailProperties.getHost(),
+                    mailProperties.getPort(),
+                    mailProperties.getUsername(),
+                    mailProperties.getPassword()
+            );
+            emailUtil.sendTextEmail(mailProperties.getAlertTo(), "微信公众号token获取失败请检查", "微信公众号token获取失败请检查");
         }
     }
 

+ 0 - 17
src/main/java/top/husj/husj_wx/utils/EmailUtil.java

@@ -51,21 +51,4 @@ public class EmailUtil {
         Transport.send(message);
     }
 
-    public static void main(String[] args) {
-        String host = "smtp.030208.xyz";
-        String port = "465";
-        String username = "husj@030208.xyz";
-        String password = "15629747218hsjH";
-        EmailUtil emailUtil = new EmailUtil(host, port, username, password);
-
-        try {
-            emailUtil.sendTextEmail("807244836@qq.com", "测试文本邮件", "这是一封测试邮件。");
-            String html = "<h1>HTML邮件测试</h1><p style='color: red;'>红色文本</p><p><a href='https://example.com'>链接</a></p>";
-            emailUtil.sendHtmlEmail("807244836@qq.com", "测试HTML邮件", html);
-            System.out.println("邮件发送成功!");
-        } catch (Exception var7) {
-            var7.printStackTrace();
-        }
-
-    }
 }

+ 7 - 0
src/main/resources/application.yml

@@ -16,3 +16,10 @@ wechat:
   appid: wxebbc42cc41337ad6
   appsecret: b0e5198cd442efe767f473f1445bd162
   encodingAESKey: TAuWfJHftr4CmGtLe8aHwkA9JVaEMCA3VoQi9VhLVpD
+
+mail:
+  host: smtp.030208.xyz
+  port: "465"
+  username: husj@030208.xyz
+  password: 15629747218hsjH
+  alert-to: 807244836@qq.com