Browse Source

邮件发送功能代码更新

fangtasyj 6 months ago
parent
commit
e1f4efba7f

+ 15 - 10
ruoyi-admin/src/main/resources/application.yml

@@ -97,13 +97,18 @@ spring:
 #    username: 399767396@qq.com  # 设置用户名
 #    password: gikwqafauevjbiag # 设置密码,该处的密码是QQ邮箱开启SMTP的授权码而非QQ密码
 #    port: 587            # 邮件服务器端口号  【163网易邮箱的是465】
-#    properties:
-#      mail:
-#        smtp:
-#          auth: true  # 必须进行授权认证,它的目的就是阻止他人任意乱发邮件
-#          starttls: # SMTP加密方式:连接到一个TLS保护连接
-#            enable: true
-#            required: true
+    protocol: smtps
+    host: smtp.163.com
+    username: pfj20155904@163.com
+    password: MWThnkJRMbqbPAXV
+    port: 465
+    properties:
+      mail:
+        smtp:
+          auth: true  # 必须进行授权认证,它的目的就是阻止他人任意乱发邮件
+          starttls: # SMTP加密方式:连接到一个TLS保护连接
+            enable: true
+            required: true
 
 # token配置
 token:
@@ -162,11 +167,11 @@ xss:
 # 各个模块提交申请信息时对应的模板文件在服务器的存储路径
 module:
   zyww:
-    templateFilePath: /Users/fangtasyj/Desktop/HHWY/智慧ZGAPP建设需求/数据库字段--20241115.xlsx   # 测试路径
+    templateFilePath: /Users/fangtasyj/Desktop/HHWY/智慧ZGAPP建设需求/zyww
   dbjz:
-    templateFilePath: /Users/fangtasyj/Desktop/HHWY/智慧ZGAPP建设需求/数据库字段--20241115.xlsx
+    templateFilePath: /Users/fangtasyj/Desktop/HHWY/智慧ZGAPP建设需求/dbjz
   hybz:
-    templateFilePath: /Users/fangtasyj/Desktop/HHWY/智慧ZGAPP建设需求/数据库字段--20241115.xlsx
+    templateFilePath: /Users/fangtasyj/Desktop/HHWY/智慧ZGAPP建设需求/hybz
 
 # 市局爬虫配置
 crawler:

+ 5 - 7
ruoyi-zzb/src/main/java/com/ruoyi/zzb/common/controller/BaseController.java

@@ -1,7 +1,6 @@
 package com.ruoyi.zzb.common.controller;
 
 import com.alibaba.fastjson.JSONObject;
-import com.ruoyi.zzb.common.utils.CommonUtils;
 import com.ruoyi.zzb.common.bean.RspResult;
 import com.ruoyi.zzb.common.service.BaseService;
 import lombok.extern.slf4j.Slf4j;
@@ -9,6 +8,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
+import java.io.File;
 
 @Slf4j
 @RestController
@@ -33,7 +33,7 @@ public class BaseController {
     public JSONObject send(@RequestBody JSONObject reqBody){
         String subject;          // 邮件主题
         String text;             // 邮件内容
-        String attachmentPath;   // 每一个模块对应的的申请模板文件在linux服务器的存储路径
+        String attachmentPath;   // 每一个模块对应的的申请模板文件在服务器的存储路径
         String toEmail = reqBody.getString("toEmail");
         if(StringUtils.isBlank(toEmail)/* || !CommonUtils.isRightEmail(toEmail)*/){
             log.error("toEmail参数非法");
@@ -49,10 +49,6 @@ public class BaseController {
             subject = "【住院慰问】";
             text = "住院慰问相关内容......";
             attachmentPath = zywwTemplateFilePath;
-            // 该模块在界面获取申请模板文件时需要选择具体的疾病种类(38种重大疾病之一),但从功能来看,对于后端业务没有实际用到该参数,待定......
-            // 后续还会在提交申请页面时选择具体的疾病种类,在该页面进行选择更合理
-//            String diseaseCode = reqBody.getString("diseaseCode");
-//            todo ???
         } else if ("dbjz".equals(moduleCode)) {
             subject = "【大病救助】";
             text = "大病救助相关内容......";
@@ -63,7 +59,9 @@ public class BaseController {
             attachmentPath = hybzTemplateFilePath;
         }
         try {
-            baseService.sendEmail(toEmail, subject, text, attachmentPath);
+            File directory = new File(attachmentPath);
+            File[] files = directory.listFiles();
+            baseService.sendEmail(toEmail, subject, text, attachmentPath, files);
             return RspResult.success("服务器发送邮件成功");
         } catch (Exception e){
             log.error("服务器发送邮件失败", e);

+ 16 - 8
ruoyi-zzb/src/main/java/com/ruoyi/zzb/common/service/BaseService.java

@@ -1,7 +1,5 @@
 package com.ruoyi.zzb.common.service;
 
-import com.baomidou.mybatisplus.core.conditions.Wrapper;
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.ruoyi.common.annotation.DataSource;
 import com.ruoyi.common.enums.DataSourceType;
 import com.ruoyi.common.utils.file.FileUploadUtils;
@@ -12,17 +10,17 @@ import com.ruoyi.zzb.common.utils.CommonUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.core.io.FileSystemResource;
 import org.springframework.mail.javamail.JavaMailSender;
 import org.springframework.mail.javamail.MimeMessageHelper;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
-
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeUtility;
 import java.io.File;
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -48,18 +46,28 @@ public class BaseService {
      * @param subject
      * @param text
      * @param attachmentPath
+     * @param files
      * @throws MessagingException
+     * @throws UnsupportedEncodingException
      */
-    public void sendEmail(String to, String subject, String text, String attachmentPath) throws MessagingException {
+    public void sendEmail(String to, String subject, String text, String attachmentPath, File[] files) throws MessagingException, UnsupportedEncodingException {
         MimeMessage message = mailSender.createMimeMessage();
+        //解决附件文件名称过长乱码问题
+        System.setProperty("mail.mime.splitlongparameters","false");
         MimeMessageHelper helper = new MimeMessageHelper(message, true);
         helper.setFrom(from);
         helper.setTo(to);
         helper.setSubject(subject);
         helper.setText(text);
-        FileSystemResource file = new FileSystemResource(new File(attachmentPath));
-        // 添加附件
-        helper.addAttachment(file.getFilename(), file);
+        // 添加附件(多个)
+        if(files != null && files.length > 0) {
+            for (File file : files) {
+                helper.addAttachment(MimeUtility.encodeWord(file.getName(), "utf-8", "B"), file);
+            }
+        }
+        /*FileSystemResource file = new FileSystemResource(new File(attachmentPath));
+        // 添加附件(单个)
+        helper.addAttachment(file.getFilename(), file);*/
         mailSender.send(message);
     }