|
@@ -1,11 +1,36 @@
|
|
|
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.MailFolder;
|
|
|
+import com.fjhx.entity.*;
|
|
|
import com.fjhx.mapper.MailFolderMapper;
|
|
|
-import com.fjhx.service.MailFolderService;
|
|
|
+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.IMAPStore;
|
|
|
+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.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 邮箱文件夹 服务实现类
|
|
@@ -14,7 +39,223 @@ import org.springframework.stereotype.Service;
|
|
|
* @author zlj
|
|
|
* @since 2023-03-06
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@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);
|
|
|
+ Message message = 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();
|
|
|
+ String contentType = part.getContentType();
|
|
|
+
|
|
|
+ String disposition = part.getDisposition();
|
|
|
+
|
|
|
+ // 处理附件
|
|
|
+ if ((disposition != null && (disposition.equalsIgnoreCase(Part.ATTACHMENT) || disposition.equalsIgnoreCase(Part.INLINE)))
|
|
|
+ || (contentType.contains("name") || contentType.contains("application"))) {
|
|
|
+
|
|
|
+ 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(message.getMailInfoId());
|
|
|
+ mailAttachmentList.add(mailAttachment);
|
|
|
+
|
|
|
+ }
|
|
|
+ // html格式
|
|
|
+ else if (part.isMimeType("text/html")) {
|
|
|
+ Object content = part.getContent();
|
|
|
+ if (content != null) {
|
|
|
+ message.setContent(content.toString());
|
|
|
+ } else {
|
|
|
+ message.setContent(StringPool.EMPTY);
|
|
|
+ }
|
|
|
+ message.setMimeType("text/html");
|
|
|
+ }
|
|
|
+ // 文本格式
|
|
|
+ else if (part.isMimeType("text/plain")) {
|
|
|
+ Object content = part.getContent();
|
|
|
+ if (content != null) {
|
|
|
+ if (ObjectUtil.notEqual(message.getMimeType(), "text/html")) {
|
|
|
+ message.setContent(content.toString());
|
|
|
+ message.setMimeType("text/plain");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 复杂体邮件
|
|
|
+ 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);
|
|
|
+ saveMessageAndAttachment(mailDetailsVo, bodyPart);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ // 未知格式
|
|
|
+ else {
|
|
|
+ log.error("未知文件格式:{} ,待解析", contentType);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文本解码
|
|
|
+ *
|
|
|
+ * @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) {
|
|
|
+
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ try {
|
|
|
+ bos.write(is.read());
|
|
|
+ ObsUpload obsUpload = new ObsUpload();
|
|
|
+ obsUpload.setFileName(fileName);
|
|
|
+ obsUpload.setBytes(bos.toByteArray());
|
|
|
+ R<JSONObject> jsonObjectR = obsClient.uploadHuaWei(obsUpload);
|
|
|
+ if (jsonObjectR.isSuccess()) {
|
|
|
+ JSONObject data = jsonObjectR.getData();
|
|
|
+ return data.getString("path");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("上传文件出错", e);
|
|
|
+ } finally {
|
|
|
+ IoUtil.close(bos);
|
|
|
+ IoUtil.close(is);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
}
|