Browse Source

update 重构 pr_172 集成邮件发送功能

疯狂的狮子Li 2 years ago
parent
commit
4941aaa5c1

+ 0 - 1
pom.xml

@@ -38,7 +38,6 @@
         <dynamic-ds.version>3.5.1</dynamic-ds.version>
         <tlog.version>1.3.6</tlog.version>
         <xxl-job.version>2.3.0</xxl-job.version>
-        <mail.version>1.6.2</mail.version>
 
         <!-- jdk11 缺失依赖 jaxb-->
         <jaxb.version>3.0.1</jaxb.version>

+ 22 - 28
ruoyi-admin/src/main/resources/application-dev.yml

@@ -137,34 +137,6 @@ spring:
     # 是否开启ssl
     ssl: false
 
-  # 邮件
-  mail:
-    enabled: false
-    # 邮件服务地址 (enabled = true 时打开该配置)
-#    host: smtp.qq.com
-    # 用户名
-    username: xxx@qq.com
-    # 授权码 (设置 - 账户 - POP3/SMTP服务)
-    password: xxx
-    # QQ邮箱加密端口,不同邮箱的端口不一样
-    port: 465
-    properties:
-      mail:
-        smtp:
-          socketFactory:
-            class: javax.net.ssl.SSLSocketFactory
-          ssl:
-            trust: smtp.qq.com
-          # 是否需要用户认证
-          auth: true
-          starttls:
-            # 启用TLS加密
-            enable: true
-            required: true
-    # 传输协议 starttls.enable = true 时为 smtps
-    protocol: smtps
-    debug: true
-
 redisson:
   # 线程池数量
   threads: 4
@@ -184,3 +156,25 @@ redisson:
     timeout: 3000
     # 发布和订阅连接池大小
     subscriptionConnectionPoolSize: 50
+
+--- # mail 邮件发送
+mail:
+  enabled: false
+  host: smtp.163.com
+  port: 465
+  # 是否需要用户名密码验证
+  auth: true
+  # 发送方,遵循RFC-822标准
+  from: xxx@163.com
+  # 用户名(注意:如果使用foxmail邮箱,此处user为qq号)
+  user: xxx@163.com
+  # 密码(注意,某些邮箱需要为SMTP服务单独设置密码,详情查看相关帮助)
+  pass: xxxxxxxxxx
+  # 使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。
+  starttlsEnable: true
+  # 使用SSL安全连接
+  sslEnable: true
+  # SMTP超时时长,单位毫秒,缺省值不超时
+  timeout: 0
+  # Socket连接超时值,单位毫秒,缺省值不超时
+  connectionTimeout: 0

+ 22 - 0
ruoyi-admin/src/main/resources/application-prod.yml

@@ -159,3 +159,25 @@ redisson:
     timeout: 3000
     # 发布和订阅连接池大小
     subscriptionConnectionPoolSize: 50
+
+--- # mail 邮件发送
+mail:
+  enabled: false
+  host: smtp.163.com
+  port: 465
+  # 是否需要用户名密码验证
+  auth: true
+  # 发送方,遵循RFC-822标准
+  from: xxx@163.com
+  # 用户名(注意:如果使用foxmail邮箱,此处user为qq号)
+  user: xxx@163.com
+  # 密码(注意,某些邮箱需要为SMTP服务单独设置密码,详情查看相关帮助)
+  pass: xxxxxxxxxx
+  # 使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。
+  starttlsEnable: true
+  # 使用SSL安全连接
+  sslEnable: true
+  # SMTP超时时长,单位毫秒,缺省值不超时
+  timeout: 0
+  # Socket连接超时值,单位毫秒,缺省值不超时
+  connectionTimeout: 0

+ 5 - 6
ruoyi-common/pom.xml

@@ -127,6 +127,11 @@
             <artifactId>hutool-extra</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>com.sun.mail</groupId>
+            <artifactId>jakarta.mail</artifactId>
+        </dependency>
+
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
@@ -159,12 +164,6 @@
             <artifactId>lock4j-redisson-spring-boot-starter</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>com.sun.mail</groupId>
-            <artifactId>javax.mail</artifactId>
-            <version>${mail.version}</version>
-        </dependency>
-
     </dependencies>
 
 </project>

+ 6 - 6
ruoyi-common/src/main/java/com/ruoyi/common/utils/email/MailUtils.java

@@ -38,13 +38,13 @@ public class MailUtils {
     /**
      * 获取邮件发送实例 (自定义发送人以及授权码)
      *
-     * @param username 发送人
-     * @param password 授权码
+     * @param user 发送人
+     * @param pass 授权码
      */
-    public static MailAccount getMailAccount(String username, String password) {
-        ACCOUNT.setFrom(StringUtils.blankToDefault(username, ACCOUNT.getUser()));
-        ACCOUNT.setUser(StringUtils.blankToDefault(username, ACCOUNT.getUser()));
-        ACCOUNT.setPass(StringUtils.blankToDefault(password, ACCOUNT.getPass()));
+    public static MailAccount getMailAccount(String from, String user, String pass) {
+        ACCOUNT.setFrom(StringUtils.blankToDefault(from, ACCOUNT.getFrom()));
+        ACCOUNT.setUser(StringUtils.blankToDefault(user, ACCOUNT.getUser()));
+        ACCOUNT.setPass(StringUtils.blankToDefault(pass, ACCOUNT.getPass()));
         return ACCOUNT;
     }
 

+ 17 - 22
ruoyi-demo/src/main/java/com/ruoyi/demo/controller/MailController.java

@@ -3,6 +3,8 @@ package com.ruoyi.demo.controller;
 import com.ruoyi.common.core.domain.R;
 import com.ruoyi.common.utils.email.MailUtils;
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
@@ -14,41 +16,34 @@ import java.io.File;
 
 
 /**
- * 测试邮件发送 Controller
+ * 邮件发送案例
  *
  * @author Michelle.Chung
  */
 @Validated
-@Api(value = "邮件控制器", tags = {"测试邮件发送"})
+@Api(value = "邮件发送案例", tags = {"邮件发送案例"})
 @RequiredArgsConstructor(onConstructor_ = @Autowired)
 @RequestMapping("/demo/mail")
 @RestController
 public class MailController {
 
-    /**
-     * 发送邮件
-     *
-     * @param to      接收人
-     * @param subject 标题
-     * @param text    内容
-     */
+    @ApiOperation("发送邮件")
     @GetMapping("/sendSimpleMessage")
-    public R<Void> sendSimpleMessage(String to, String subject, String text) {
-        MailUtils.send(to, subject, text, false);
-        return R.ok("操作成功");
+    public R<Void> sendSimpleMessage(@ApiParam("接收人") String to,
+                                     @ApiParam("标题") String subject,
+                                     @ApiParam("内容") String text) {
+        MailUtils.sendText(to, subject, text);
+        return R.ok();
     }
 
-    /**
-     * 发送邮件(带附件)
-     *
-     * @param to      接收人
-     * @param subject 标题
-     * @param text    内容
-     */
+    @ApiOperation("发送邮件(带附件)")
     @GetMapping("/sendMessageWithAttachment")
-    public R<Void> sendMessageWithAttachment(String to, String subject, String text, String filePath) {
-        MailUtils.send(to, subject, text, false, new File(filePath));
-        return R.ok("操作成功");
+    public R<Void> sendMessageWithAttachment(@ApiParam("接收人") String to,
+                                             @ApiParam("标题") String subject,
+                                             @ApiParam("内容") String text,
+                                             @ApiParam("附件路径") String filePath) {
+        MailUtils.sendText(to, subject, text, new File(filePath));
+        return R.ok();
     }
 
 }

+ 10 - 14
ruoyi-framework/src/main/java/com/ruoyi/framework/config/MailConfig.java

@@ -6,8 +6,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
-import javax.annotation.Resource;
-
 /**
  * JavaMail 配置
  *
@@ -16,23 +14,21 @@ import javax.annotation.Resource;
 @Configuration
 public class MailConfig {
 
-    @Resource
-    private MailProperties mailProperties;
-
-    /**
-     * 初始化 JavaMailSender
-     */
     @Bean
-    @ConditionalOnProperty(value = "spring.mail.enabled", havingValue = "true")
-    public MailAccount mailAccount() {
+    @ConditionalOnProperty(value = "mail.enabled", havingValue = "true")
+    public MailAccount mailAccount(MailProperties mailProperties) {
         MailAccount account = new MailAccount();
-        account.setFrom(mailProperties.getUsername());
-        account.setUser(mailProperties.getUsername());
-        account.setPass(mailProperties.getPassword());
+        account.setHost(mailProperties.getHost());
         account.setPort(mailProperties.getPort());
         account.setAuth(mailProperties.getAuth());
-        account.setDebug(mailProperties.getDebug());
+        account.setFrom(mailProperties.getFrom());
+        account.setUser(mailProperties.getUser());
+        account.setPass(mailProperties.getPass());
+        account.setSocketFactoryPort(mailProperties.getPort());
         account.setStarttlsEnable(mailProperties.getStarttlsEnable());
+        account.setSslEnable(mailProperties.getSslEnable());
+        account.setTimeout(mailProperties.getTimeout());
+        account.setConnectionTimeout(mailProperties.getConnectionTimeout());
         return account;
     }
 

+ 26 - 20
ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/MailProperties.java

@@ -1,7 +1,6 @@
 package com.ruoyi.framework.config.properties;
 
 import lombok.Data;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 
@@ -12,7 +11,7 @@ import org.springframework.stereotype.Component;
  */
 @Data
 @Component
-@ConfigurationProperties(prefix = "spring.mail")
+@ConfigurationProperties(prefix = "mail")
 public class MailProperties {
 
     /**
@@ -21,45 +20,52 @@ public class MailProperties {
     private String enabled;
 
     /**
-     * 邮件服务地址
+     * SMTP服务器域名
      */
     private String host;
 
     /**
-     * 用户名
+     * SMTP服务端口
      */
-    private String username;
+    private Integer port;
 
     /**
-     * 授权码 (设置 - 账户 - POP3/SMTP服务)
+     * 是否需要用户名密码验证
      */
-    private String password;
+    private Boolean auth;
 
     /**
-     * 邮箱加密端口,不同邮箱的端口不一样
+     * 用户名
      */
-    private Integer port;
+    private String user;
 
     /**
-     * 是否需要用户认证
+     * 密码
      */
-    @Value("${spring.mail.properties.mail.smtp.auth}")
-    private Boolean auth;
+    private String pass;
 
     /**
-     * 是否启用TLS加密
+     * 发送方,遵循RFC-822标准
      */
-    @Value("${spring.mail.properties.mail.smtp.starttls.enable}")
-    private Boolean starttlsEnable;
+    private String from;
 
-    @Value("${spring.mail.properties.mail.smtp.ssl.trust}")
-    private String sslTrust;
+    /**
+     * 使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。它将纯文本连接升级为加密连接(TLS或SSL), 而不是使用一个单独的加密通信端口。
+     */
+    private Boolean starttlsEnable;
 
-    private Boolean debug;
+    /**
+     * 使用 SSL安全连接
+     */
+    private Boolean sslEnable;
 
     /**
-     * 传输协议 starttls.enable = true 时为 smtps
+     * SMTP超时时长,单位毫秒,缺省值不超时
      */
-    private String protocol;
+    private Long timeout;
 
+    /**
+     * Socket连接超时值,单位毫秒,缺省值不超时
+     */
+    private Long connectionTimeout;
 }