|
@@ -11,6 +11,7 @@ import com.fjhx.service.*;
|
|
import com.fjhx.utils.EmailUtil;
|
|
import com.fjhx.utils.EmailUtil;
|
|
import com.fjhx.vo.MailDetailsVo;
|
|
import com.fjhx.vo.MailDetailsVo;
|
|
import com.sun.mail.imap.IMAPFolder;
|
|
import com.sun.mail.imap.IMAPFolder;
|
|
|
|
+import com.sun.mail.imap.IMAPMessage;
|
|
import com.sun.mail.imap.IMAPStore;
|
|
import com.sun.mail.imap.IMAPStore;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springblade.core.log.exception.ServiceException;
|
|
import org.springblade.core.log.exception.ServiceException;
|
|
@@ -23,10 +24,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.mail.*;
|
|
import javax.mail.*;
|
|
import javax.mail.internet.MimeUtility;
|
|
import javax.mail.internet.MimeUtility;
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
|
-import java.io.IOException;
|
|
|
|
-import java.io.InputStream;
|
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
|
|
|
+import java.io.*;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
@@ -75,14 +73,16 @@ public class MailFolderServiceImpl extends ServiceImpl<MailFolderMapper, MailFol
|
|
Integer attachmentSync = mailInfo.getAttachmentSync();
|
|
Integer attachmentSync = mailInfo.getAttachmentSync();
|
|
|
|
|
|
if (ObjectUtil.notEqual(messageSync, 1) || ObjectUtil.notEqual(addressSync, 1) || ObjectUtil.notEqual(attachmentSync, 1)) {
|
|
if (ObjectUtil.notEqual(messageSync, 1) || ObjectUtil.notEqual(addressSync, 1) || ObjectUtil.notEqual(attachmentSync, 1)) {
|
|
|
|
+
|
|
Mailbox mailbox = mailboxService.getById(mailInfo.getMailboxId());
|
|
Mailbox mailbox = mailboxService.getById(mailInfo.getMailboxId());
|
|
IMAPStore imapStore = null;
|
|
IMAPStore imapStore = null;
|
|
IMAPFolder folder = null;
|
|
IMAPFolder folder = null;
|
|
|
|
+
|
|
try {
|
|
try {
|
|
imapStore = EmailUtil.getIMAPStore(mailbox.getMailHost(), mailbox.getMailboxName(), mailbox.getMailboxPwd());
|
|
imapStore = EmailUtil.getIMAPStore(mailbox.getMailHost(), mailbox.getMailboxName(), mailbox.getMailboxPwd());
|
|
folder = (IMAPFolder) imapStore.getFolder(mailInfo.getFolderName());
|
|
folder = (IMAPFolder) imapStore.getFolder(mailInfo.getFolderName());
|
|
folder.open(Folder.READ_ONLY);
|
|
folder.open(Folder.READ_ONLY);
|
|
- Message message = folder.getMessage(mailInfo.getMessageNumber());
|
|
|
|
|
|
+ IMAPMessage message = (IMAPMessage) folder.getMessage(mailInfo.getMessageNumber());
|
|
|
|
|
|
// 同步地址
|
|
// 同步地址
|
|
if (ObjectUtil.notEqual(addressSync, 1)) {
|
|
if (ObjectUtil.notEqual(addressSync, 1)) {
|
|
@@ -165,12 +165,15 @@ public class MailFolderServiceImpl extends ServiceImpl<MailFolderMapper, MailFol
|
|
if (part.isMimeType("text/plain")) {
|
|
if (part.isMimeType("text/plain")) {
|
|
addPlain(part, message);
|
|
addPlain(part, message);
|
|
}
|
|
}
|
|
|
|
+
|
|
// 正文:html格式
|
|
// 正文:html格式
|
|
else if (part.isMimeType("text/html")) {
|
|
else if (part.isMimeType("text/html")) {
|
|
addHtml(part, message);
|
|
addHtml(part, message);
|
|
}
|
|
}
|
|
|
|
+
|
|
// 大对象
|
|
// 大对象
|
|
else if (part.isMimeType("multipart/*")) {
|
|
else if (part.isMimeType("multipart/*")) {
|
|
|
|
+
|
|
// 复杂体邮件
|
|
// 复杂体邮件
|
|
Multipart multipart = (Multipart) part.getContent();
|
|
Multipart multipart = (Multipart) part.getContent();
|
|
|
|
|
|
@@ -178,8 +181,10 @@ public class MailFolderServiceImpl extends ServiceImpl<MailFolderMapper, MailFol
|
|
int partCount = multipart.getCount();
|
|
int partCount = multipart.getCount();
|
|
|
|
|
|
for (int i = 0; i < partCount; i++) {
|
|
for (int i = 0; i < partCount; i++) {
|
|
|
|
+
|
|
// 获得复杂体邮件中其中一个邮件体
|
|
// 获得复杂体邮件中其中一个邮件体
|
|
BodyPart bodyPart = multipart.getBodyPart(i);
|
|
BodyPart bodyPart = multipart.getBodyPart(i);
|
|
|
|
+
|
|
// 某一个邮件体也有可能是由多个邮件体组成的复杂体
|
|
// 某一个邮件体也有可能是由多个邮件体组成的复杂体
|
|
String disposition = bodyPart.getDisposition();
|
|
String disposition = bodyPart.getDisposition();
|
|
|
|
|
|
@@ -202,13 +207,14 @@ public class MailFolderServiceImpl extends ServiceImpl<MailFolderMapper, MailFol
|
|
else if (bodyPart.isMimeType("multipart/*")) {
|
|
else if (bodyPart.isMimeType("multipart/*")) {
|
|
saveMessageAndAttachment(mailDetailsVo, bodyPart);
|
|
saveMessageAndAttachment(mailDetailsVo, bodyPart);
|
|
}
|
|
}
|
|
|
|
+
|
|
// 其他类型
|
|
// 其他类型
|
|
else {
|
|
else {
|
|
String contentType = bodyPart.getContentType();
|
|
String contentType = bodyPart.getContentType();
|
|
if (contentType.contains("name") || contentType.contains("application")) {
|
|
if (contentType.contains("name") || contentType.contains("application")) {
|
|
addFile(bodyPart, mailAttachmentList, message.getMailInfoId());
|
|
addFile(bodyPart, mailAttachmentList, message.getMailInfoId());
|
|
} else {
|
|
} else {
|
|
- log.error("未知文件格式:{} ,待解析", contentType);
|
|
|
|
|
|
+ log.error("未知文件格式:{} ,邮件id:{},待解析", contentType, message.getMailInfoId());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -220,7 +226,7 @@ public class MailFolderServiceImpl extends ServiceImpl<MailFolderMapper, MailFol
|
|
}
|
|
}
|
|
// 未知格式
|
|
// 未知格式
|
|
else {
|
|
else {
|
|
- log.error("未知文件格式:{} ,待解析", part.getContentType());
|
|
|
|
|
|
+ log.error("未知文件格式:{} ,邮件id:{},待解析", part.getContentType(), message.getMailInfoId());
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
@@ -277,12 +283,24 @@ public class MailFolderServiceImpl extends ServiceImpl<MailFolderMapper, MailFol
|
|
*/
|
|
*/
|
|
private String uploadFile(InputStream is, String fileName) {
|
|
private String uploadFile(InputStream is, String fileName) {
|
|
|
|
|
|
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(is);
|
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
+ BufferedOutputStream bos = new BufferedOutputStream(baos);
|
|
|
|
+
|
|
try {
|
|
try {
|
|
|
|
+ int len;
|
|
|
|
+
|
|
|
|
+ while ((len = bis.read()) != -1) {
|
|
|
|
+ bos.write(len);
|
|
|
|
+ bos.flush();
|
|
|
|
+ }
|
|
|
|
+
|
|
bos.write(is.read());
|
|
bos.write(is.read());
|
|
|
|
+
|
|
|
|
+ // 上次附件至华为云
|
|
ObsUpload obsUpload = new ObsUpload();
|
|
ObsUpload obsUpload = new ObsUpload();
|
|
obsUpload.setFileName(fileName);
|
|
obsUpload.setFileName(fileName);
|
|
- obsUpload.setBytes(bos.toByteArray());
|
|
|
|
|
|
+ obsUpload.setBytes(baos.toByteArray());
|
|
R<JSONObject> jsonObjectR = obsClient.uploadHuaWei(obsUpload);
|
|
R<JSONObject> jsonObjectR = obsClient.uploadHuaWei(obsUpload);
|
|
if (jsonObjectR.isSuccess()) {
|
|
if (jsonObjectR.isSuccess()) {
|
|
JSONObject data = jsonObjectR.getData();
|
|
JSONObject data = jsonObjectR.getData();
|
|
@@ -290,8 +308,11 @@ public class MailFolderServiceImpl extends ServiceImpl<MailFolderMapper, MailFol
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("上传文件出错", e);
|
|
log.error("上传文件出错", e);
|
|
|
|
+ throw new ServiceException("获取附件出现错误");
|
|
} finally {
|
|
} finally {
|
|
IoUtil.close(bos);
|
|
IoUtil.close(bos);
|
|
|
|
+ IoUtil.close(baos);
|
|
|
|
+ IoUtil.close(bis);
|
|
IoUtil.close(is);
|
|
IoUtil.close(is);
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|