|
@@ -22,10 +22,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
-import javax.mail.Folder;
|
|
|
-import javax.mail.FolderNotFoundException;
|
|
|
-import javax.mail.Message;
|
|
|
-import javax.mail.MessagingException;
|
|
|
+import javax.mail.*;
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
@@ -45,21 +42,10 @@ public class CoreServiceImpl {
|
|
|
@Autowired
|
|
|
private MailInfoService mailInfoService;
|
|
|
|
|
|
- // private final MailAddressService mailAddressService;
|
|
|
- // private final MailMessageService mailMessageService;
|
|
|
- // private final MailAttachmentService mailAttachmentService;
|
|
|
-
|
|
|
@Autowired
|
|
|
@Qualifier(TaskPoolConfig.emailTaskExecutor)
|
|
|
private Executor emailTaskExecutor;
|
|
|
|
|
|
- // @Autowired
|
|
|
- // private PlatformTransactionManager platformTransactionManager;
|
|
|
- //
|
|
|
- // @Autowired
|
|
|
- // private TransactionDefinition transactionDefinition;
|
|
|
-
|
|
|
-
|
|
|
// 累计同步次数
|
|
|
private static int num = 0;
|
|
|
// 单次同步邮件总次数
|
|
@@ -80,11 +66,11 @@ public class CoreServiceImpl {
|
|
|
try {
|
|
|
synchronousMail(mailbox);
|
|
|
} catch (Exception e) {
|
|
|
- log.error("这里出错了", e);
|
|
|
+ log.error("发生未知异常", e);
|
|
|
}
|
|
|
|
|
|
countDownLatch.countDown();
|
|
|
- System.out.println("countDownLatch.getCount() = " + countDownLatch.getCount());
|
|
|
+ log.info("第 {} 伦同步还剩余 {} 封邮箱需要同步", num, countDownLatch.getCount());
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -116,7 +102,8 @@ public class CoreServiceImpl {
|
|
|
|
|
|
// 开启抓取邮件任务
|
|
|
for (Mailbox mailbox : list) {
|
|
|
- // System.out.println("循环了" + i++ + "次");
|
|
|
+
|
|
|
+ // // 创建异步任务
|
|
|
// Task task = new Task(countDownLatch, mailbox);
|
|
|
// // 开启异步操作
|
|
|
// emailTaskExecutor.execute(task);
|
|
@@ -127,7 +114,7 @@ public class CoreServiceImpl {
|
|
|
// try {
|
|
|
// countDownLatch.await();
|
|
|
// } catch (InterruptedException e) {
|
|
|
- // throw new RuntimeException(e);
|
|
|
+ // log.error("countDownLatch.await() 发生异常", e);
|
|
|
// }
|
|
|
|
|
|
// 结束处理时间
|
|
@@ -154,23 +141,21 @@ public class CoreServiceImpl {
|
|
|
*/
|
|
|
private void synchronousMail(Mailbox mailbox) {
|
|
|
|
|
|
- // 获取所有文件夹
|
|
|
- List<MailFolder> mailFolderList = mailFolderService.list(
|
|
|
- Wrappers.<MailFolder>lambdaQuery().eq(MailFolder::getMailboxId, mailbox.getId()));
|
|
|
-
|
|
|
IMAPStore store = null;
|
|
|
|
|
|
try {
|
|
|
+
|
|
|
+ // 获取邮件连接
|
|
|
store = EmailUtil.getIMAPStore(mailbox.getMailHost(), mailbox.getMailboxName(), mailbox.getMailboxPwd());
|
|
|
|
|
|
- // 文件夹为空,代表初始化,从官方邮箱重新拉取文件夹
|
|
|
+ // 获取所有文件夹
|
|
|
+ List<MailFolder> mailFolderList = mailFolderService.list(
|
|
|
+ Wrappers.<MailFolder>lambdaQuery().eq(MailFolder::getMailboxId, mailbox.getId()));
|
|
|
+
|
|
|
+ // 文件夹为空,代表初始化,从官方邮箱拉取文件夹
|
|
|
if (mailFolderList.size() == 0) {
|
|
|
mailFolderList = EmailUtil.createMailFolderList(store, mailbox.getId());
|
|
|
- try {
|
|
|
- mailFolderService.saveBatch(mailFolderList);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ mailFolderService.saveBatch(mailFolderList);
|
|
|
}
|
|
|
|
|
|
// 分别拉取每个文件夹邮件
|
|
@@ -181,22 +166,28 @@ public class CoreServiceImpl {
|
|
|
// 只读方式打开收件箱
|
|
|
folder.open(Folder.READ_ONLY);
|
|
|
} catch (FolderNotFoundException folderNotFoundException) {
|
|
|
- // 邮箱不存在,则删除文件夹记录
|
|
|
+ // 邮箱不存在异常,则删除文件夹
|
|
|
mailFolderService.removeById(mailFolder.getId());
|
|
|
continue;
|
|
|
}
|
|
|
+
|
|
|
try {
|
|
|
+ // 获取文件夹邮件
|
|
|
Message[] messages = folder.getMessages();
|
|
|
if (messages.length == 0) {
|
|
|
- break;
|
|
|
+ continue;
|
|
|
}
|
|
|
|
|
|
+ // 如果邮件最后一封和最后一次同步id相同,则代表没有新邮件
|
|
|
IMAPMessage message = (IMAPMessage) messages[messages.length - 1];
|
|
|
if (Objects.equals(message.getMessageID(), mailFolder.getLastMessageId())) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
+ // 拉取邮件
|
|
|
List<MailInfo> mailInfoList = getMailInfoList(mailFolder, messages);
|
|
|
+
|
|
|
+ // 保存最后一份邮件id与收件时间
|
|
|
mailFolder.setLastMessageId(message.getMessageID());
|
|
|
mailFolder.setLastReceivedDate(message.getReceivedDate());
|
|
|
saveMailInfo(mailFolder, mailInfoList);
|
|
@@ -209,7 +200,7 @@ public class CoreServiceImpl {
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
- log.error("链接imap出错,邮箱信息:{}", JSON.toJSONString(mailbox), e);
|
|
|
+ log.error("邮件同步出错,邮箱信息:{}", JSON.toJSONString(mailbox), e);
|
|
|
} finally {
|
|
|
if (store != null) {
|
|
|
try {
|
|
@@ -223,11 +214,17 @@ public class CoreServiceImpl {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 拉取邮件
|
|
|
+ */
|
|
|
private List<MailInfo> getMailInfoList(MailFolder mailFolder, Message[] messages) throws MessagingException {
|
|
|
+ // 获取最后一次发件id
|
|
|
String lastMessageId = mailFolder.getLastMessageId();
|
|
|
+ // 获取最好一次收件的发送时间
|
|
|
Date lastReceivedDate = ObjectUtil.defaultIfNull(mailFolder.getLastReceivedDate(),
|
|
|
DateUtil.offsetDay(new Date(), -initDay));
|
|
|
|
|
|
+ // 收件列表
|
|
|
List<MailInfo> mailInfoList = new ArrayList<>();
|
|
|
|
|
|
int length = messages.length;
|
|
@@ -238,15 +235,18 @@ public class CoreServiceImpl {
|
|
|
// 邮件id
|
|
|
String messageID = message.getMessageID();
|
|
|
|
|
|
+ // 最后一封邮件id相同,代表上次同步邮件到这,不继续找之前的邮件了
|
|
|
if (ObjectUtil.isNotEmpty(lastMessageId)) {
|
|
|
if (lastMessageId.equals(messageID)) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
+ // 收件时间在上次收件时间之前,不继续找之前的邮件了
|
|
|
if (receivedDate.before(lastReceivedDate)) {
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
+ // 邮件信息
|
|
|
MailInfo mailInfo = new MailInfo();
|
|
|
mailInfo.setMailboxId(mailFolder.getMailboxId());
|
|
|
mailInfo.setMessageId(messageID);
|
|
@@ -255,19 +255,22 @@ public class CoreServiceImpl {
|
|
|
mailInfo.setFlags(EmailUtil.getFlags(message.getFlags()));
|
|
|
mailInfo.setFolderId(mailFolder.getId());
|
|
|
mailInfo.setFolderName(mailFolder.getName());
|
|
|
+ mailInfo.setSendDate(receivedDate);
|
|
|
+ mailInfo.setCreateTime(new Date());
|
|
|
|
|
|
-
|
|
|
- InternetAddress[] internetAddresses = (InternetAddress[]) message.getFrom();
|
|
|
- if (internetAddresses.length > 0) {
|
|
|
- InternetAddress internetAddress = internetAddresses[0];
|
|
|
+ // 保存发件人信息
|
|
|
+ Address[] addresses = message.getFrom();
|
|
|
+ if (addresses.length > 0) {
|
|
|
+ InternetAddress internetAddress = (InternetAddress) addresses[0];
|
|
|
mailInfo.setFromEmail(internetAddress.getAddress());
|
|
|
mailInfo.setFromPersonalName(internetAddress.getPersonal());
|
|
|
mailInfo.setFromType(internetAddress.getType());
|
|
|
}
|
|
|
- mailInfo.setSendDate(receivedDate);
|
|
|
- mailInfo.setCreateTime(new Date());
|
|
|
+
|
|
|
+ // 添加到邮件列表
|
|
|
mailInfoList.add(0, mailInfo);
|
|
|
|
|
|
+ // 添加同步邮件数量
|
|
|
addMailCount();
|
|
|
}
|
|
|
|
|
@@ -275,25 +278,16 @@ public class CoreServiceImpl {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 保存邮件数据
|
|
|
+ * 保存邮件数据,更新文件夹最后同步邮件时间
|
|
|
*/
|
|
|
private void saveMailInfo(MailFolder mailFolder, List<MailInfo> mailInfoList) {
|
|
|
- // TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
|
|
|
- // try {
|
|
|
-
|
|
|
mailFolderService.updateById(mailFolder);
|
|
|
mailInfoService.saveBatch(mailInfoList);
|
|
|
-
|
|
|
- // 事务提交
|
|
|
- // platformTransactionManager.commit(transactionStatus);
|
|
|
- // } catch (Exception e) {
|
|
|
- // log.error("同步邮箱发生错误", e);
|
|
|
- // // 事务回滚
|
|
|
- // platformTransactionManager.rollback(transactionStatus);
|
|
|
- // }
|
|
|
}
|
|
|
|
|
|
- // 添加同步邮件数量
|
|
|
+ /**
|
|
|
+ * 添加同步邮件数量
|
|
|
+ */
|
|
|
private static synchronized void addMailCount() {
|
|
|
mailCount++;
|
|
|
}
|