24282 пре 2 година
родитељ
комит
19ef68ec74

+ 1 - 1
bladex-saas-project/new-mail/src/main/java/com/fjhx/back/controller/TestController.java

@@ -2,10 +2,10 @@ package com.fjhx.back.controller;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.fjhx.back.entity.MailboxListMessagesDto;
 import com.fjhx.back.wy163.rsa.HttpPost;
 import com.fjhx.back.wy163.rsa.RSASignatureToQiye;
 import com.fjhx.back.wy163.rsa.RSATool;
-import com.fjhx.back.entity.MailboxListMessagesDto;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springblade.core.tool.api.R;

+ 8 - 1
bladex-saas-project/new-mail/src/main/java/com/fjhx/back/entity/Message.java

@@ -10,40 +10,47 @@ public class Message {
      * 发件人
      */
     private String from;
+
     /**
      * 标题
      */
     private String subject;
+
     /**
      * 收件人
      */
     private String to;
+
     /**
      * 邮件内容类型
      */
     private String contentType;
+
     /**
      * 邮件内容(摘要)
      */
     private String content;
+
     /**
      * 邮件大小
      */
     private String mailSizeByte;
+
     /**
      * 发信时间
      */
     private String sentDate;
+
     /**
      * 字符集
      */
     private String charset;
+
     /**
      * 附件
      */
     private String attachment;
 
-
     @JSONField(name = "From")
     public void setFrom(String from) {
         this.from = from;

+ 4 - 2
bladex-saas-project/new-mail/src/main/java/com/fjhx/controller/MailInfoController.java

@@ -48,7 +48,6 @@ public class MailInfoController {
         return R.data(list);
     }
 
-
     /**
      * 获取文件夹列表
      */
@@ -77,10 +76,13 @@ public class MailInfoController {
      */
     @GetMapping("/details")
     public R details(@RequestParam("mailId") Long mailId) {
-        MailDetailsVo mailDetailsVo = mailFolderService.getDetails(mailId);
+        MailDetailsVo mailDetailsVo = mailInfoService.getDetails(mailId);
         return R.data(mailDetailsVo);
     }
 
+    /**
+     * 发送邮件
+     */
     @PostMapping("/send")
     public R send(@RequestBody SendDto dto) {
         mailInfoService.send(dto);

+ 3 - 0
bladex-saas-project/new-mail/src/main/java/com/fjhx/dto/SendDto.java

@@ -19,14 +19,17 @@ public class SendDto {
      * 发送
      */
     private List<Address> to;
+
     /**
      * 抄送
      */
     private List<Address> cc;
+
     /**
      * 密送
      */
     private List<Address> bcc;
+
     /**
      * 回复
      */

+ 0 - 1
bladex-saas-project/new-mail/src/main/java/com/fjhx/entity/MailAttachment.java

@@ -3,7 +3,6 @@ package com.fjhx.entity;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
-import org.springblade.core.mp.base.BasicsEntity;
 import lombok.Getter;
 import lombok.Setter;
 

+ 0 - 3
bladex-saas-project/new-mail/src/main/java/com/fjhx/service/MailFolderService.java

@@ -2,7 +2,6 @@ package com.fjhx.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.fjhx.entity.MailFolder;
-import com.fjhx.vo.MailDetailsVo;
 
 /**
  * <p>
@@ -14,6 +13,4 @@ import com.fjhx.vo.MailDetailsVo;
  */
 public interface MailFolderService extends IService<MailFolder> {
 
-    MailDetailsVo getDetails(Long mailId);
-
 }

+ 3 - 0
bladex-saas-project/new-mail/src/main/java/com/fjhx/service/MailInfoService.java

@@ -3,6 +3,7 @@ package com.fjhx.service;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.fjhx.dto.SendDto;
 import com.fjhx.entity.MailInfo;
+import com.fjhx.vo.MailDetailsVo;
 
 /**
  * <p>
@@ -14,6 +15,8 @@ import com.fjhx.entity.MailInfo;
  */
 public interface MailInfoService extends IService<MailInfo> {
 
+    MailDetailsVo getDetails(Long mailId);
+
     void send(SendDto dto);
 
 }

+ 20 - 16
bladex-saas-project/new-mail/src/main/java/com/fjhx/service/impl/CoreServiceImpl.java

@@ -37,8 +37,10 @@ public class CoreServiceImpl {
 
     @Autowired
     private MailboxService mailboxService;
+
     @Autowired
     private MailFolderService mailFolderService;
+
     @Autowired
     private MailInfoService mailInfoService;
 
@@ -151,6 +153,8 @@ public class CoreServiceImpl {
      */
     private void synchronousMail(Mailbox mailbox) {
 
+        String mailboxId = mailbox.getId();
+
         IMAPStore store = null;
 
         try {
@@ -159,8 +163,8 @@ public class CoreServiceImpl {
             store = EmailUtil.getIMAPStore(mailbox.getMailHost(), mailbox.getMailboxName(), mailbox.getMailboxPwd());
 
             // 获取所有文件夹
-            List<MailFolder> mailFolderList = mailFolderService.list(Wrappers.<MailFolder>lambdaQuery()
-                    .eq(MailFolder::getMailboxId, mailbox.getId()));
+            List<MailFolder> mailFolderList = mailFolderService.list(
+                    Wrappers.<MailFolder>lambdaQuery().eq(MailFolder::getMailboxId, mailbox.getId()));
 
             // 文件夹为空,代表初始化,从官方邮箱拉取文件夹
             if (mailFolderList.size() == 0) {
@@ -209,8 +213,21 @@ public class CoreServiceImpl {
                 }
             }
 
+            map.remove(mailboxId);
+
         } catch (AuthenticationFailedException | MailConnectException exception) {
-            disableMailRead(mailbox.getId());
+
+            map.merge(mailboxId, 1, Integer::sum);
+
+            Integer num = map.get(mailboxId);
+            if (num >= 3) {
+                mailboxService.update(Wrappers.<Mailbox>lambdaUpdate()
+                        .eq(Mailbox::getId, mailboxId)
+                        .set(Mailbox::getStatus, 1)
+                        .set(Mailbox::getUpdateTime, new Date()));
+                map.remove(mailboxId);
+            }
+
         } catch (Exception e) {
             log.error("邮件同步出错,邮箱信息:{}", JSON.toJSONString(mailbox), e);
         } finally {
@@ -226,19 +243,6 @@ public class CoreServiceImpl {
         }
     }
 
-    private void disableMailRead(String mailboxId) {
-        map.merge(mailboxId, 1, Integer::sum);
-
-        Integer num = map.get(mailboxId);
-        if (num >= 3) {
-            mailboxService.update(Wrappers.<Mailbox>lambdaUpdate()
-                    .eq(Mailbox::getId, mailboxId)
-                    .set(Mailbox::getStatus, 1)
-                    .set(Mailbox::getUpdateTime, new Date()));
-            map.remove(mailboxId);
-        }
-    }
-
     /**
      * 拉取邮件
      */

+ 0 - 38
bladex-saas-project/new-mail/src/main/java/com/fjhx/service/impl/MailAttachmentServiceImpl.java

@@ -5,7 +5,6 @@ import com.fjhx.entity.MailAttachment;
 import com.fjhx.mapper.MailAttachmentMapper;
 import com.fjhx.service.MailAttachmentService;
 import lombok.AllArgsConstructor;
-import org.springblade.resource.feign.IObsClient;
 import org.springframework.stereotype.Service;
 
 /**
@@ -20,41 +19,4 @@ import org.springframework.stereotype.Service;
 @AllArgsConstructor
 public class MailAttachmentServiceImpl extends ServiceImpl<MailAttachmentMapper, MailAttachment> implements MailAttachmentService {
 
-    private IObsClient obsClient;
-
-    // @Override
-    // public List<MailAttachment> getMailAttachment(Long mailInfoId, ReceivedEmail receivedEmail) {
-    //     List<EmailAttachment<? extends DataSource>> attachments = receivedEmail.attachments();
-    //
-    //     List<MailAttachment> mailAttachmentList = new ArrayList<>();
-    //     for (EmailAttachment<? extends DataSource> attachment : attachments) {
-    //
-    //         String name = attachment.getName();
-    //
-    //         ObsUpload obsUpload = new ObsUpload();
-    //         obsUpload.setFileName(name);
-    //         obsUpload.setBytes(attachment.toByteArray());
-    //
-    //         try {
-    //             R<JSONObject> jsonObjectR = obsClient.uploadHuaWei(obsUpload);
-    //             boolean success = jsonObjectR.isSuccess();
-    //             if (!success) {
-    //                 continue;
-    //             }
-    //             JSONObject data = jsonObjectR.getData();
-    //
-    //             MailAttachment mailAttachment = new MailAttachment();
-    //             mailAttachment.setName(name);
-    //             mailAttachment.setUrl(data.getString("path"));
-    //             mailAttachmentList.add(mailAttachment);
-    //
-    //         } catch (Exception e) {
-    //             throw new RuntimeException(e);
-    //         }
-    //
-    //     }
-    //
-    //     return mailAttachmentList;
-    //
-    // }
 }

+ 2 - 301
bladex-saas-project/new-mail/src/main/java/com/fjhx/service/impl/MailFolderServiceImpl.java

@@ -1,34 +1,12 @@
 package com.fjhx.service.impl;
 
-import cn.hutool.core.io.IoUtil;
-import cn.hutool.core.util.ObjectUtil;
-import com.alibaba.fastjson.JSONObject;
-import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.fjhx.entity.*;
+import com.fjhx.entity.MailFolder;
 import com.fjhx.mapper.MailFolderMapper;
-import com.fjhx.service.*;
-import com.fjhx.utils.EmailUtil;
-import com.fjhx.vo.MailDetailsVo;
-import com.sun.mail.imap.IMAPFolder;
-import com.sun.mail.imap.IMAPMessage;
-import com.sun.mail.imap.IMAPStore;
+import com.fjhx.service.MailFolderService;
 import lombok.extern.slf4j.Slf4j;
-import org.springblade.core.log.exception.ServiceException;
-import org.springblade.core.tool.api.R;
-import org.springblade.core.tool.utils.StringPool;
-import org.springblade.resource.entity.ObsUpload;
-import org.springblade.resource.feign.IObsClient;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import javax.mail.*;
-import javax.mail.internet.MimeUtility;
-import java.io.*;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
-
 /**
  * <p>
  * 邮箱文件夹 服务实现类
@@ -41,281 +19,4 @@ import java.util.Objects;
 @Service
 public class MailFolderServiceImpl extends ServiceImpl<MailFolderMapper, MailFolder> implements MailFolderService {
 
-    @Autowired
-    private MailInfoService mailInfoService;
-
-    @Autowired
-    private MailAddressService mailAddressService;
-
-    @Autowired
-    private MailboxService mailboxService;
-
-    @Autowired
-    private MailMessageService mailMessageService;
-
-    @Autowired
-    private MailAttachmentService mailAttachmentService;
-
-    @Autowired
-    private IObsClient obsClient;
-
-    @Override
-    public MailDetailsVo getDetails(Long mailId) {
-        MailDetailsVo mailDetailsVo = new MailDetailsVo();
-
-        MailInfo mailInfo = mailInfoService.getById(mailId);
-        if (mailInfo == null) {
-            throw new ServiceException("没有找到邮件信息");
-        }
-
-        Integer messageSync = mailInfo.getMessageSync();
-        Integer addressSync = mailInfo.getAddressSync();
-        Integer attachmentSync = mailInfo.getAttachmentSync();
-
-        if (ObjectUtil.notEqual(messageSync, 1) || ObjectUtil.notEqual(addressSync, 1) || ObjectUtil.notEqual(attachmentSync, 1)) {
-
-            Mailbox mailbox = mailboxService.getById(mailInfo.getMailboxId());
-            IMAPStore imapStore = null;
-            IMAPFolder folder = null;
-
-            try {
-                imapStore = EmailUtil.getIMAPStore(mailbox.getMailHost(), mailbox.getMailboxName(), mailbox.getMailboxPwd());
-                folder = (IMAPFolder) imapStore.getFolder(mailInfo.getFolderName());
-                folder.open(Folder.READ_ONLY);
-                IMAPMessage message = (IMAPMessage) folder.getMessage(mailInfo.getMessageNumber());
-
-                // 同步地址
-                if (ObjectUtil.notEqual(addressSync, 1)) {
-                    List<MailAddress> mailAddressList = EmailUtil.mailAddressList(message, mailId);
-                    mailDetailsVo.setMailAddressList(mailAddressList);
-                    new Thread(() -> {
-                        mailAddressService.saveBatch(mailAddressList);
-                        mailInfoService.update(Wrappers.<MailInfo>lambdaUpdate()
-                                .eq(MailInfo::getId, mailId).set(MailInfo::getAddressSync, 1));
-                    }).start();
-                }
-
-                // 附件正文
-                if (ObjectUtil.notEqual(messageSync, 1) || ObjectUtil.notEqual(attachmentSync, 1)) {
-                    MailMessage mailMessage = new MailMessage();
-                    mailMessage.setMailInfoId(mailId);
-
-                    List<MailAttachment> mailAttachmentList = new ArrayList<>();
-
-                    mailDetailsVo.setMessage(mailMessage);
-                    mailDetailsVo.setMailAttachmentList(mailAttachmentList);
-
-                    saveMessageAndAttachment(mailDetailsVo, message);
-
-                    new Thread(() -> {
-                        mailMessageService.save(mailMessage);
-                        mailAttachmentService.saveBatch(mailAttachmentList);
-
-                        mailInfoService.update(Wrappers.<MailInfo>lambdaUpdate().eq(MailInfo::getId, mailId)
-                                .set(MailInfo::getMessageSync, 1).set(MailInfo::getAttachmentSync, 1));
-                    }).start();
-
-                }
-
-            } catch (Exception e) {
-                log.error("获取邮件出错", e);
-                throw new ServiceException("获取邮件出错");
-            } finally {
-                try {
-                    if (imapStore != null) {
-                        if (imapStore.isConnected()) {
-                            imapStore.close();
-                        }
-                    }
-                    if (folder != null) {
-                        if (folder.isOpen()) {
-                            folder.close();
-                        }
-                    }
-                } catch (MessagingException e) {
-                    log.error("关闭链接失败", e);
-                }
-            }
-        }
-
-        if (Objects.equals(addressSync, 1)) {
-            List<MailAddress> mailAddressList = mailAddressService.list(Wrappers.<MailAddress>lambdaQuery().eq(MailAddress::getMailInfoId, mailId));
-            mailDetailsVo.setMailAddressList(mailAddressList);
-        }
-        if (Objects.equals(messageSync, 1)) {
-            List<MailMessage> list = mailMessageService.list(Wrappers.<MailMessage>lambdaQuery().eq(MailMessage::getMailInfoId, mailId));
-            if (list.size() > 0)
-                mailDetailsVo.setMessage(list.get(0));
-        }
-        if (Objects.equals(attachmentSync, 1)) {
-            List<MailAttachment> list = mailAttachmentService.list(Wrappers.<MailAttachment>lambdaQuery().eq(MailAttachment::getMailInfoId, mailId));
-            mailDetailsVo.setMailAttachmentList(list);
-        }
-
-        return mailDetailsVo;
-    }
-
-
-    // 保存附件和正文
-    private void saveMessageAndAttachment(MailDetailsVo mailDetailsVo, Part part) throws MessagingException, IOException {
-        MailMessage message = mailDetailsVo.getMessage();
-        List<MailAttachment> mailAttachmentList = mailDetailsVo.getMailAttachmentList();
-
-        // 正文:文本格式
-        if (part.isMimeType("text/plain")) {
-            addPlain(part, message);
-        }
-
-        // 正文:html格式
-        else if (part.isMimeType("text/html")) {
-            addHtml(part, message);
-        }
-
-        // 大对象
-        else if (part.isMimeType("multipart/*")) {
-
-            // 复杂体邮件
-            Multipart multipart = (Multipart) part.getContent();
-
-            // 复杂体邮件包含多个邮件体
-            int partCount = multipart.getCount();
-
-            for (int i = 0; i < partCount; i++) {
-
-                // 获得复杂体邮件中其中一个邮件体
-                BodyPart bodyPart = multipart.getBodyPart(i);
-
-                // 某一个邮件体也有可能是由多个邮件体组成的复杂体
-                String disposition = bodyPart.getDisposition();
-
-                // 正文:html格式
-                if (bodyPart.isMimeType("text/html")) {
-                    addHtml(bodyPart, message);
-                }
-
-                // 正文:文本格式
-                if (bodyPart.isMimeType("text/plain")) {
-                    addPlain(bodyPart, message);
-                }
-
-                // 附件
-                else if (disposition != null && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE))) {
-                    addFile(bodyPart, mailAttachmentList, message.getMailInfoId());
-                }
-
-                // 大对象
-                else if (bodyPart.isMimeType("multipart/*")) {
-                    saveMessageAndAttachment(mailDetailsVo, bodyPart);
-                }
-
-                // 其他类型
-                else {
-                    String contentType = bodyPart.getContentType();
-                    if (contentType.contains("name") || contentType.contains("application")) {
-                        addFile(bodyPart, mailAttachmentList, message.getMailInfoId());
-                    } else {
-                        log.error("未知文件格式:{} ,邮件id:{},待解析", contentType, message.getMailInfoId());
-                    }
-                }
-
-            }
-        }
-        // rfc822
-        else if (part.isMimeType("message/rfc822")) {
-            saveMessageAndAttachment(mailDetailsVo, (Part) part.getContent());
-        }
-        // 未知格式
-        else {
-            log.error("未知文件格式:{} ,邮件id:{},待解析", part.getContentType(), message.getMailInfoId());
-        }
-
-    }
-
-    private void addFile(Part part, List<MailAttachment> mailAttachmentList, Long mailInfoId) throws MessagingException, IOException {
-        InputStream is = part.getInputStream();
-        String fileName = decodeText(part.getFileName());
-
-        String path = uploadFile(is, fileName);
-        MailAttachment mailAttachment = new MailAttachment();
-        mailAttachment.setName(fileName);
-        mailAttachment.setUrl(path);
-        mailAttachment.setMailInfoId(mailInfoId);
-        mailAttachmentList.add(mailAttachment);
-    }
-
-    private void addHtml(Part part, MailMessage message) throws MessagingException, IOException {
-        Object content = part.getContent();
-        if (content != null) {
-            message.setContent(content.toString());
-        } else {
-            message.setContent(StringPool.EMPTY);
-        }
-        message.setMimeType("text/html");
-    }
-
-    private void addPlain(Part part, MailMessage message) throws MessagingException, IOException {
-        Object content = part.getContent();
-        if (content != null) {
-            if (ObjectUtil.notEqual(message.getMimeType(), "text/html")) {
-                message.setContent(content.toString());
-                message.setMimeType("text/plain");
-            }
-        }
-    }
-
-
-    /**
-     * 文本解码
-     *
-     * @param encodeText 解码MimeUtility.encodeText(String text)方法编码后的文本
-     * @return 解码后的文本
-     */
-    private String decodeText(String encodeText) throws UnsupportedEncodingException {
-        if (encodeText == null || StringPool.EMPTY.equals(encodeText)) {
-            return StringPool.EMPTY;
-        } else {
-            return MimeUtility.decodeText(encodeText);
-        }
-    }
-
-    /**
-     * 上传文件
-     */
-    private String uploadFile(InputStream is, String fileName) {
-
-        BufferedInputStream bis = new BufferedInputStream(is);
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        BufferedOutputStream bos = new BufferedOutputStream(baos);
-
-        try {
-            int len;
-
-            while ((len = bis.read()) != -1) {
-                bos.write(len);
-                bos.flush();
-            }
-
-            bos.write(is.read());
-
-            // 上次附件至华为云
-            ObsUpload obsUpload = new ObsUpload();
-            obsUpload.setFileName(fileName);
-            obsUpload.setBytes(baos.toByteArray());
-            R<JSONObject> jsonObjectR = obsClient.uploadHuaWei(obsUpload);
-            if (jsonObjectR.isSuccess()) {
-                JSONObject data = jsonObjectR.getData();
-                return data.getString("path");
-            }
-        } catch (Exception e) {
-            log.error("上传文件出错", e);
-            throw new ServiceException("获取附件出现错误");
-        } finally {
-            IoUtil.close(bos);
-            IoUtil.close(baos);
-            IoUtil.close(bis);
-            IoUtil.close(is);
-        }
-        return null;
-    }
-
 }

+ 292 - 4
bladex-saas-project/new-mail/src/main/java/com/fjhx/service/impl/MailInfoServiceImpl.java

@@ -1,20 +1,37 @@
 package com.fjhx.service.impl;
 
+import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.util.ObjectUtil;
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fjhx.dto.SendDto;
-import com.fjhx.entity.MailInfo;
-import com.fjhx.entity.Mailbox;
+import com.fjhx.entity.*;
 import com.fjhx.mapper.MailInfoMapper;
-import com.fjhx.service.MailInfoService;
-import com.fjhx.service.MailboxService;
+import com.fjhx.service.*;
 import com.fjhx.utils.EmailUtil;
+import com.fjhx.vo.MailDetailsVo;
+import com.sun.mail.imap.IMAPFolder;
+import com.sun.mail.imap.IMAPMessage;
+import com.sun.mail.imap.IMAPStore;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springblade.core.log.exception.ServiceException;
+import org.springblade.core.tool.api.R;
+import org.springblade.core.tool.utils.StringPool;
+import org.springblade.resource.entity.ObsUpload;
+import org.springblade.resource.feign.IObsClient;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import javax.mail.*;
+import javax.mail.internet.MimeUtility;
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
 /**
  * <p>
  * 邮件主表 服务实现类
@@ -31,6 +48,117 @@ public class MailInfoServiceImpl extends ServiceImpl<MailInfoMapper, MailInfo> i
     @Autowired
     private MailboxService mailboxService;
 
+    @Autowired
+    private MailInfoService mailInfoService;
+
+    @Autowired
+    private MailAddressService mailAddressService;
+
+    @Autowired
+    private MailMessageService mailMessageService;
+
+    @Autowired
+    private MailAttachmentService mailAttachmentService;
+
+    @Autowired
+    private IObsClient obsClient;
+
+    @Override
+    public MailDetailsVo getDetails(Long mailId) {
+        MailDetailsVo mailDetailsVo = new MailDetailsVo();
+
+        MailInfo mailInfo = mailInfoService.getById(mailId);
+        if (mailInfo == null) {
+            throw new ServiceException("没有找到邮件信息");
+        }
+
+        Integer messageSync = mailInfo.getMessageSync();
+        Integer addressSync = mailInfo.getAddressSync();
+        Integer attachmentSync = mailInfo.getAttachmentSync();
+
+        if (ObjectUtil.notEqual(messageSync, 1) || ObjectUtil.notEqual(addressSync, 1) || ObjectUtil.notEqual(attachmentSync, 1)) {
+
+            Mailbox mailbox = mailboxService.getById(mailInfo.getMailboxId());
+            IMAPStore imapStore = null;
+            IMAPFolder folder = null;
+
+            try {
+                imapStore = EmailUtil.getIMAPStore(mailbox.getMailHost(), mailbox.getMailboxName(), mailbox.getMailboxPwd());
+                folder = (IMAPFolder) imapStore.getFolder(mailInfo.getFolderName());
+                folder.open(Folder.READ_ONLY);
+                IMAPMessage message = (IMAPMessage) folder.getMessage(mailInfo.getMessageNumber());
+
+                // 同步地址
+                if (ObjectUtil.notEqual(addressSync, 1)) {
+                    List<MailAddress> mailAddressList = EmailUtil.mailAddressList(message, mailId);
+                    mailDetailsVo.setMailAddressList(mailAddressList);
+                    new Thread(() -> {
+                        mailAddressService.saveBatch(mailAddressList);
+                        mailInfoService.update(Wrappers.<MailInfo>lambdaUpdate()
+                                .eq(MailInfo::getId, mailId).set(MailInfo::getAddressSync, 1));
+                    }).start();
+                }
+
+                // 附件正文
+                if (ObjectUtil.notEqual(messageSync, 1) || ObjectUtil.notEqual(attachmentSync, 1)) {
+                    MailMessage mailMessage = new MailMessage();
+                    mailMessage.setMailInfoId(mailId);
+
+                    List<MailAttachment> mailAttachmentList = new ArrayList<>();
+
+                    mailDetailsVo.setMessage(mailMessage);
+                    mailDetailsVo.setMailAttachmentList(mailAttachmentList);
+
+                    saveMessageAndAttachment(mailDetailsVo, message);
+
+                    new Thread(() -> {
+                        mailMessageService.save(mailMessage);
+                        mailAttachmentService.saveBatch(mailAttachmentList);
+
+                        mailInfoService.update(Wrappers.<MailInfo>lambdaUpdate().eq(MailInfo::getId, mailId)
+                                .set(MailInfo::getMessageSync, 1).set(MailInfo::getAttachmentSync, 1));
+                    }).start();
+
+                }
+
+            } catch (Exception e) {
+                log.error("获取邮件出错", e);
+                throw new ServiceException("获取邮件出错");
+            } finally {
+                try {
+                    if (imapStore != null) {
+                        if (imapStore.isConnected()) {
+                            imapStore.close();
+                        }
+                    }
+                    if (folder != null) {
+                        if (folder.isOpen()) {
+                            folder.close();
+                        }
+                    }
+                } catch (MessagingException e) {
+                    log.error("关闭链接失败", e);
+                }
+            }
+        }
+
+        if (Objects.equals(addressSync, 1)) {
+            List<MailAddress> mailAddressList = mailAddressService.list(Wrappers.<MailAddress>lambdaQuery().eq(MailAddress::getMailInfoId, mailId));
+            mailDetailsVo.setMailAddressList(mailAddressList);
+        }
+        if (Objects.equals(messageSync, 1)) {
+            List<MailMessage> list = mailMessageService.list(Wrappers.<MailMessage>lambdaQuery().eq(MailMessage::getMailInfoId, mailId));
+            if (list.size() > 0)
+                mailDetailsVo.setMessage(list.get(0));
+        }
+        if (Objects.equals(attachmentSync, 1)) {
+            List<MailAttachment> list = mailAttachmentService.list(Wrappers.<MailAttachment>lambdaQuery().eq(MailAttachment::getMailInfoId, mailId));
+            mailDetailsVo.setMailAttachmentList(list);
+        }
+
+        return mailDetailsVo;
+    }
+
     @Override
     public void send(SendDto dto) {
         String mailboxId = dto.getMailboxId();
@@ -45,4 +173,164 @@ public class MailInfoServiceImpl extends ServiceImpl<MailInfoMapper, MailInfo> i
 
     }
 
+    /**
+     * 保存附件和正文
+     */
+    private void saveMessageAndAttachment(MailDetailsVo mailDetailsVo, Part part) throws MessagingException, IOException {
+        MailMessage message = mailDetailsVo.getMessage();
+        List<MailAttachment> mailAttachmentList = mailDetailsVo.getMailAttachmentList();
+
+        // 正文:文本格式
+        if (part.isMimeType("text/plain")) {
+            addPlain(part, message);
+        }
+
+        // 正文:html格式
+        else if (part.isMimeType("text/html")) {
+            addHtml(part, message);
+        }
+
+        // 大对象
+        else if (part.isMimeType("multipart/*")) {
+
+            // 复杂体邮件
+            Multipart multipart = (Multipart) part.getContent();
+
+            // 复杂体邮件包含多个邮件体
+            int partCount = multipart.getCount();
+
+            for (int i = 0; i < partCount; i++) {
+
+                // 获得复杂体邮件中其中一个邮件体
+                BodyPart bodyPart = multipart.getBodyPart(i);
+
+                // 某一个邮件体也有可能是由多个邮件体组成的复杂体
+                String disposition = bodyPart.getDisposition();
+
+                // 正文:html格式
+                if (bodyPart.isMimeType("text/html")) {
+                    addHtml(bodyPart, message);
+                }
+
+                // 正文:文本格式
+                if (bodyPart.isMimeType("text/plain")) {
+                    addPlain(bodyPart, message);
+                }
+
+                // 附件
+                else if (disposition != null && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE))) {
+                    addFile(bodyPart, mailAttachmentList, message.getMailInfoId());
+                }
+
+                // 大对象
+                else if (bodyPart.isMimeType("multipart/*")) {
+                    saveMessageAndAttachment(mailDetailsVo, bodyPart);
+                }
+
+                // 其他类型
+                else {
+                    String contentType = bodyPart.getContentType();
+                    if (contentType.contains("name") || contentType.contains("application")) {
+                        addFile(bodyPart, mailAttachmentList, message.getMailInfoId());
+                    } else {
+                        log.error("未知文件格式:{} ,邮件id:{},待解析", contentType, message.getMailInfoId());
+                    }
+                }
+
+            }
+        }
+        // rfc822
+        else if (part.isMimeType("message/rfc822")) {
+            saveMessageAndAttachment(mailDetailsVo, (Part) part.getContent());
+        }
+        // 未知格式
+        else {
+            log.error("未知文件格式:{} ,邮件id:{},待解析", part.getContentType(), message.getMailInfoId());
+        }
+
+    }
+
+    private void addFile(Part part, List<MailAttachment> mailAttachmentList, Long mailInfoId) throws MessagingException, IOException {
+        InputStream is = part.getInputStream();
+        String fileName = decodeText(part.getFileName());
+
+        String path = uploadFile(is, fileName);
+        MailAttachment mailAttachment = new MailAttachment();
+        mailAttachment.setName(fileName);
+        mailAttachment.setUrl(path);
+        mailAttachment.setMailInfoId(mailInfoId);
+        mailAttachmentList.add(mailAttachment);
+    }
+
+    private void addHtml(Part part, MailMessage message) throws MessagingException, IOException {
+        Object content = part.getContent();
+        if (content != null) {
+            message.setContent(content.toString());
+        } else {
+            message.setContent(StringPool.EMPTY);
+        }
+        message.setMimeType("text/html");
+    }
+
+    private void addPlain(Part part, MailMessage message) throws MessagingException, IOException {
+        Object content = part.getContent();
+        if (content != null) {
+            if (ObjectUtil.notEqual(message.getMimeType(), "text/html")) {
+                message.setContent(content.toString());
+                message.setMimeType("text/plain");
+            }
+        }
+    }
+
+    /**
+     * 文本解码
+     */
+    private String decodeText(String encodeText) throws UnsupportedEncodingException {
+        if (encodeText == null || StringPool.EMPTY.equals(encodeText)) {
+            return StringPool.EMPTY;
+        } else {
+            return MimeUtility.decodeText(encodeText);
+        }
+    }
+
+    /**
+     * 上传文件
+     */
+    private String uploadFile(InputStream is, String fileName) {
+
+        BufferedInputStream bis = new BufferedInputStream(is);
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        BufferedOutputStream bos = new BufferedOutputStream(baos);
+
+        try {
+            int len;
+
+            while ((len = bis.read()) != -1) {
+                bos.write(len);
+                bos.flush();
+            }
+
+            bos.write(is.read());
+
+            // 上次附件至华为云
+            ObsUpload obsUpload = new ObsUpload();
+            obsUpload.setFileName(fileName);
+            obsUpload.setBytes(baos.toByteArray());
+            R<JSONObject> jsonObjectR = obsClient.uploadHuaWei(obsUpload);
+            if (jsonObjectR.isSuccess()) {
+                JSONObject data = jsonObjectR.getData();
+                return data.getString("path");
+            }
+        } catch (Exception e) {
+            log.error("上传文件出错", e);
+            throw new ServiceException("获取附件出现错误");
+        } finally {
+            IoUtil.close(bos);
+            IoUtil.close(baos);
+            IoUtil.close(bis);
+            IoUtil.close(is);
+        }
+        return null;
+    }
+
 }

+ 0 - 1
bladex-saas-project/new-mail/src/main/java/com/fjhx/service/impl/MailMessageServiceImpl.java

@@ -17,5 +17,4 @@ import org.springframework.stereotype.Service;
 @Service
 public class MailMessageServiceImpl extends ServiceImpl<MailMessageMapper, MailMessage> implements MailMessageService {
 
-
 }

+ 1 - 2
bladex-saas-project/new-mail/src/main/java/com/fjhx/vo/MailDetailsVo.java

@@ -10,11 +10,10 @@ import java.util.List;
 @Data
 public class MailDetailsVo {
 
+    MailMessage message;
 
     List<MailAddress> mailAddressList;
 
-    MailMessage message;
-
     List<MailAttachment> mailAttachmentList;
 
 }