|
|
|
@ -1,17 +1,33 @@ |
|
|
|
|
package com.dxhy.core.task; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.codec.Base64Decoder; |
|
|
|
|
import cn.hutool.core.util.IdUtil; |
|
|
|
|
import com.dxhy.common.constant.DbConstant; |
|
|
|
|
import com.dxhy.common.datasource.config.DynamicContextHolder; |
|
|
|
|
import com.dxhy.core.job.entity.ScheduleJobEntity; |
|
|
|
|
import com.dxhy.core.job.service.ScheduleJobService; |
|
|
|
|
import com.dxhy.core.model.mailGather.EmailMaintainVo; |
|
|
|
|
import com.dxhy.core.model.mailGather.MailGatherLogVo; |
|
|
|
|
import com.dxhy.core.model.openservice.OpenServiceOcr; |
|
|
|
|
import com.dxhy.core.service.mailGather.EmailMaintainService; |
|
|
|
|
import com.dxhy.core.service.mailGather.MailGatherLogService; |
|
|
|
|
import com.dxhy.core.service.openservice.IOpenServicePlatformService; |
|
|
|
|
import com.dxhy.core.util.EmailParseUtils; |
|
|
|
|
import com.google.common.collect.Maps; |
|
|
|
|
import com.sun.mail.pop3.POP3Folder; |
|
|
|
|
import lombok.SneakyThrows; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
|
import javax.mail.*; |
|
|
|
|
import javax.mail.internet.MimeMessage; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Properties; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -21,17 +37,202 @@ public class SnEmailGatherTask { |
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
private ScheduleJobService scheduleJobService; |
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
private EmailMaintainService emailMaintainService; |
|
|
|
|
@Resource |
|
|
|
|
private MailGatherLogService mailGatherLogService; |
|
|
|
|
@Resource |
|
|
|
|
private IOpenServicePlatformService openServicePlatformService; |
|
|
|
|
|
|
|
|
|
public void mailGatherTask(){ |
|
|
|
|
//进行邮件采集
|
|
|
|
|
DynamicContextHolder.push(DbConstant.BASICS_READ); |
|
|
|
|
ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("snEmailGatherTask", "getInvoices"); |
|
|
|
|
|
|
|
|
|
if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) { |
|
|
|
|
try { |
|
|
|
|
scheduleJobEntity.setJobStatus("1"); |
|
|
|
|
DynamicContextHolder.push(DbConstant.BASICS_WRITE); |
|
|
|
|
scheduleJobService.updateById(scheduleJobEntity); |
|
|
|
|
//进行邮件采集
|
|
|
|
|
List<EmailMaintainVo> maintainVos = emailMaintainService.queryAll(); |
|
|
|
|
for (EmailMaintainVo maintainVo : maintainVos) { |
|
|
|
|
//挨个邮箱进行数据采集
|
|
|
|
|
parseEmail(maintainVo); |
|
|
|
|
} |
|
|
|
|
log.info("发票获取接口执行结束--"); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} finally { |
|
|
|
|
log.info("发票获取接口--重置执行状态"); |
|
|
|
|
scheduleJobEntity.setJobStatus("0"); |
|
|
|
|
DynamicContextHolder.push(DbConstant.BASICS_WRITE); |
|
|
|
|
scheduleJobService.updateById(scheduleJobEntity); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void parseEmail(EmailMaintainVo maintainVo){ |
|
|
|
|
long startTime = System.currentTimeMillis(); |
|
|
|
|
String password = Base64Decoder.decodeStr(maintainVo.getEmailPassword()); |
|
|
|
|
String emailName = maintainVo.getEmailAddress(); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
Store store = mailAuth(emailName, password); |
|
|
|
|
if(store == null){ |
|
|
|
|
MailGatherLogVo gatherLogVo = new MailGatherLogVo(); |
|
|
|
|
gatherLogVo.setId(IdUtil.nanoId()); |
|
|
|
|
gatherLogVo.setErrorMsg("邮箱认证失败"); |
|
|
|
|
gatherLogVo.setOcrType("1"); |
|
|
|
|
gatherLogVo.setCreateTime(new Date()); |
|
|
|
|
gatherLogVo.setFromAddress(emailName); |
|
|
|
|
mailGatherLogService.insert(gatherLogVo); |
|
|
|
|
} |
|
|
|
|
Folder folder = store.getFolder("INBOX"); |
|
|
|
|
//获取邮件列表
|
|
|
|
|
folder.open(Folder.READ_WRITE); |
|
|
|
|
// 由于POP3协议无法获知邮件的状态,所以getUnreadMessageCount得到的是收件箱的邮件总数
|
|
|
|
|
log.debug("未读邮件数: " + folder.getUnreadMessageCount()); |
|
|
|
|
// 由于POP3协议无法获知邮件的状态,所以下面得到的结果始终都是为0
|
|
|
|
|
log.debug("删除邮件数: " + folder.getDeletedMessageCount()); |
|
|
|
|
log.debug("新邮件: " + folder.getNewMessageCount()); |
|
|
|
|
// 获得收件箱中的邮件总数
|
|
|
|
|
log.debug("邮件总数: " + folder.getMessageCount()); |
|
|
|
|
EmailMaintainVo emailMaintainVo = new EmailMaintainVo(); |
|
|
|
|
emailMaintainVo.setId(maintainVo.getId()); |
|
|
|
|
Message[] messages = folder.getMessages(); |
|
|
|
|
for (int i = 0, count = messages.length; i < count; i++) { |
|
|
|
|
MimeMessage msg = (MimeMessage) messages[i]; |
|
|
|
|
log.debug("------------------解析第" + msg.getMessageNumber() + "封邮件-------------------- "); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SneakyThrows |
|
|
|
|
MailGatherLogVo gatherLogVo = new MailGatherLogVo(); |
|
|
|
|
gatherLogVo.setId(IdUtil.nanoId()); |
|
|
|
|
gatherLogVo.setSubject(EmailParseUtils.getSubject(msg)); |
|
|
|
|
gatherLogVo.setFromAddress(emailName); |
|
|
|
|
gatherLogVo.setReceiveAddress(EmailParseUtils.getReceiveAddress(msg, null)); |
|
|
|
|
gatherLogVo.setSentDate( EmailParseUtils.getSentDate(msg, null)); |
|
|
|
|
gatherLogVo.setCreateTime(new Date()); |
|
|
|
|
|
|
|
|
|
log.debug("主题: " + EmailParseUtils.getSubject(msg)); |
|
|
|
|
log.debug("发件人: " + EmailParseUtils.getFrom(msg)); |
|
|
|
|
log.debug("收件人:" + EmailParseUtils.getReceiveAddress(msg, null)); |
|
|
|
|
log.debug("发送时间:" + EmailParseUtils.getSentDate(msg, null)); |
|
|
|
|
log.debug("是否已读:" + EmailParseUtils.isSeen(msg)); |
|
|
|
|
log.debug("邮件优先级:" + EmailParseUtils.getPriority(msg)); |
|
|
|
|
log.debug("是否需要回执:" + EmailParseUtils.isReplySign(msg)); |
|
|
|
|
log.debug("邮件大小:" + msg.getSize() * 1024 + "kb"); |
|
|
|
|
|
|
|
|
|
POP3Folder inbox = (POP3Folder) folder; |
|
|
|
|
String uid = inbox.getUID(msg); |
|
|
|
|
if(StringUtils.isBlank(maintainVo.getEmailUid()) && i == 0){ |
|
|
|
|
emailMaintainVo.setEmailUid(uid); |
|
|
|
|
} |
|
|
|
|
//增量处理 等于之后就跳过本地循环
|
|
|
|
|
if(StringUtils.isNotBlank(maintainVo.getEmailUid()) && maintainVo.getEmailUid().equals(uid)){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
gatherLogVo.setMsgUid(uid); |
|
|
|
|
boolean isContainerAttachment = EmailParseUtils.isContainAttachment(msg); |
|
|
|
|
if(isContainerAttachment){ |
|
|
|
|
Map<String, String> map = Maps.newHashMap(); |
|
|
|
|
EmailParseUtils.saveAttachment(msg, map); |
|
|
|
|
|
|
|
|
|
gatherLogVo.setFileType(map.get("contentType")); |
|
|
|
|
gatherLogVo.setFileName(map.get("fileName")); |
|
|
|
|
String pdfStream = map.get("pdfStream"); |
|
|
|
|
List<OpenServiceOcr> openServiceOcrs = openServicePlatformService.ocrInvoice(pdfStream); |
|
|
|
|
if(openServiceOcrs != null && !openServiceOcrs.isEmpty()){ |
|
|
|
|
openServiceOcrs.get(0).getData().getFPDM(); |
|
|
|
|
gatherLogVo.setFpdm( openServiceOcrs.get(0).getData().getFPDM()); |
|
|
|
|
gatherLogVo.setFphm( openServiceOcrs.get(0).getData().getFPHM()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}else { //无附件的不处理
|
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Flags flags = msg.getFlags();
|
|
|
|
|
// if (!flags.contains(Flags.Flag.SEEN)) {
|
|
|
|
|
// //设置为已读
|
|
|
|
|
// msg.setFlag(Flags.Flag.SEEN, true);
|
|
|
|
|
// }
|
|
|
|
|
//进行数据插入
|
|
|
|
|
if(StringUtils.isNotBlank(gatherLogVo.getFpdm()) && StringUtils.isNotBlank(gatherLogVo.getFphm())){ |
|
|
|
|
gatherLogVo.setOcrType("0"); |
|
|
|
|
gatherLogVo.setErrorMsg("采集成功"); |
|
|
|
|
mailGatherLogService.insert(gatherLogVo); |
|
|
|
|
}else { |
|
|
|
|
log.info("未采集到有效数据"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 释放资源
|
|
|
|
|
folder.close(false); // false为不更新邮件,true为更新,一般在删除邮件后使用
|
|
|
|
|
store.close(); |
|
|
|
|
log.info("邮箱采集使用时间:{}",System.currentTimeMillis() - startTime); |
|
|
|
|
} catch (MessagingException e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Store mailAuth(String emailName, String password){ |
|
|
|
|
//判断是否为QQ还是163
|
|
|
|
|
//邮件接收协议
|
|
|
|
|
String mail_protocol = "mail.store.protocol"; |
|
|
|
|
//邮件接收协议类型
|
|
|
|
|
String mail_protocol_type2 = "pop3"; |
|
|
|
|
String mail_protocol_type = "pop"; |
|
|
|
|
//邮件接收服务器端口
|
|
|
|
|
String mail_port = "mail.pop3.port"; |
|
|
|
|
//邮件接收服务器端口
|
|
|
|
|
String port = "110"; |
|
|
|
|
//邮件接收服务器地址
|
|
|
|
|
String mail_host = "mail.pop3.host"; |
|
|
|
|
String mailSuffix = emailName.split("@")[1]; |
|
|
|
|
//邮箱类型
|
|
|
|
|
String mailType = mailSuffix.split("\\.")[0]; |
|
|
|
|
Properties props = new Properties(); |
|
|
|
|
if(mailType.equalsIgnoreCase("qq")){ |
|
|
|
|
//qq邮箱
|
|
|
|
|
props.setProperty(mail_protocol, mail_protocol_type2); // 协议
|
|
|
|
|
props.setProperty(mail_host, mail_protocol_type+"."+mailSuffix); // pop3服务器
|
|
|
|
|
props.setProperty(mail_port, port); // 端口
|
|
|
|
|
}else { |
|
|
|
|
props.setProperty(mail_protocol, mail_protocol_type2); // 协议
|
|
|
|
|
props.setProperty(mail_host, mail_protocol_type2+"."+mailSuffix); // pop3服务器
|
|
|
|
|
props.setProperty(mail_port, port); // 端口
|
|
|
|
|
} |
|
|
|
|
Session session = Session.getInstance(props); |
|
|
|
|
//连接邮箱服务器
|
|
|
|
|
|
|
|
|
|
// 创建Session实例对象
|
|
|
|
|
// Session session = Session.getInstance(props,new Authenticator() {
|
|
|
|
|
// protected PasswordAuthentication getPasswordAuthentication() {
|
|
|
|
|
// return new PasswordAuthentication(emailName, password);
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
//Session session = Session.getInstance(props);
|
|
|
|
|
Store store = null; |
|
|
|
|
try { |
|
|
|
|
store = session.getStore("pop3"); |
|
|
|
|
store.connect(emailName, password); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
try { |
|
|
|
|
store.close(); |
|
|
|
|
} catch (MessagingException ex) { |
|
|
|
|
throw new RuntimeException(ex); |
|
|
|
|
} |
|
|
|
|
store = null; |
|
|
|
|
} |
|
|
|
|
return store; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
//邮件接收协议
|
|
|
|
|
String mail_protocol = "mail.store.protocol"; |
|
|
|
@ -61,51 +262,63 @@ public class SnEmailGatherTask { |
|
|
|
|
props.setProperty(mail_port, port); // 端口
|
|
|
|
|
} |
|
|
|
|
Session session = Session.getInstance(props); |
|
|
|
|
Store store = session.getStore("pop3"); |
|
|
|
|
//连接邮箱服务器
|
|
|
|
|
store.connect(emailName, password); |
|
|
|
|
//获取当前时间
|
|
|
|
|
Date currentTime = new Date(); |
|
|
|
|
// 获得收件箱 pop3协议只有一个有效的文件夹就是INBOX
|
|
|
|
|
Folder folder = store.getFolder("INBOX"); |
|
|
|
|
//获取邮件列表
|
|
|
|
|
folder.open(Folder.READ_WRITE); |
|
|
|
|
// 由于POP3协议无法获知邮件的状态,所以getUnreadMessageCount得到的是收件箱的邮件总数
|
|
|
|
|
System.out.println("未读邮件数: " + folder.getUnreadMessageCount()); |
|
|
|
|
// 由于POP3协议无法获知邮件的状态,所以下面得到的结果始终都是为0
|
|
|
|
|
System.out.println("删除邮件数: " + folder.getDeletedMessageCount()); |
|
|
|
|
System.out.println("新邮件: " + folder.getNewMessageCount()); |
|
|
|
|
// 获得收件箱中的邮件总数
|
|
|
|
|
System.out.println("邮件总数: " + folder.getMessageCount()); |
|
|
|
|
Message[] messages = folder.getMessages(); |
|
|
|
|
for (int i = 0, count = messages.length; i < count; i++) { |
|
|
|
|
MimeMessage msg = (MimeMessage) messages[i]; |
|
|
|
|
System.out.println("------------------解析第" + msg.getMessageNumber() + "封邮件-------------------- "); |
|
|
|
|
System.out.println("主题: " + EmailParseUtils.getSubject(msg)); |
|
|
|
|
System.out.println("发件人: " + EmailParseUtils.getFrom(msg)); |
|
|
|
|
System.out.println("收件人:" + EmailParseUtils.getReceiveAddress(msg, null)); |
|
|
|
|
System.out.println("发送时间:" + EmailParseUtils.getSentDate(msg, null)); |
|
|
|
|
System.out.println("是否已读:" + EmailParseUtils.isSeen(msg)); |
|
|
|
|
System.out.println("邮件优先级:" + EmailParseUtils.getPriority(msg)); |
|
|
|
|
System.out.println("是否需要回执:" + EmailParseUtils.isReplySign(msg)); |
|
|
|
|
System.out.println("邮件大小:" + msg.getSize() * 1024 + "kb"); |
|
|
|
|
boolean isContainerAttachment = EmailParseUtils.isContainAttachment(msg); |
|
|
|
|
POP3Folder inbox = (POP3Folder) folder; |
|
|
|
|
String uid = inbox.getUID(msg); |
|
|
|
|
System.out.println("uid: ------------------" + uid); |
|
|
|
|
Flags flags = msg.getFlags(); |
|
|
|
|
if (!flags.contains(Flags.Flag.SEEN)) { |
|
|
|
|
//设置为已读
|
|
|
|
|
msg.setFlag(Flags.Flag.SEEN, true); |
|
|
|
|
Store store = null; |
|
|
|
|
try { |
|
|
|
|
store = session.getStore("pop3"); |
|
|
|
|
store.connect(emailName, password); |
|
|
|
|
//连接邮箱服务器
|
|
|
|
|
//获取当前时间
|
|
|
|
|
Date currentTime = new Date(); |
|
|
|
|
// 获得收件箱 pop3协议只有一个有效的文件夹就是INBOX
|
|
|
|
|
Folder folder = store.getFolder("INBOX"); |
|
|
|
|
//获取邮件列表
|
|
|
|
|
folder.open(Folder.READ_WRITE); |
|
|
|
|
// 由于POP3协议无法获知邮件的状态,所以getUnreadMessageCount得到的是收件箱的邮件总数
|
|
|
|
|
System.out.println("未读邮件数: " + folder.getUnreadMessageCount()); |
|
|
|
|
// 由于POP3协议无法获知邮件的状态,所以下面得到的结果始终都是为0
|
|
|
|
|
System.out.println("删除邮件数: " + folder.getDeletedMessageCount()); |
|
|
|
|
System.out.println("新邮件: " + folder.getNewMessageCount()); |
|
|
|
|
// 获得收件箱中的邮件总数
|
|
|
|
|
System.out.println("邮件总数: " + folder.getMessageCount()); |
|
|
|
|
Message[] messages = folder.getMessages(); |
|
|
|
|
for (int i = 0, count = messages.length; i < count; i++) { |
|
|
|
|
MimeMessage msg = (MimeMessage) messages[i]; |
|
|
|
|
System.out.println("------------------解析第" + msg.getMessageNumber() + "封邮件-------------------- "); |
|
|
|
|
System.out.println("主题: " + EmailParseUtils.getSubject(msg)); |
|
|
|
|
System.out.println("发件人: " + EmailParseUtils.getFrom(msg)); |
|
|
|
|
System.out.println("收件人:" + EmailParseUtils.getReceiveAddress(msg, null)); |
|
|
|
|
System.out.println("发送时间:" + EmailParseUtils.getSentDate(msg, null)); |
|
|
|
|
System.out.println("是否已读:" + EmailParseUtils.isSeen(msg)); |
|
|
|
|
System.out.println("邮件优先级:" + EmailParseUtils.getPriority(msg)); |
|
|
|
|
System.out.println("是否需要回执:" + EmailParseUtils.isReplySign(msg)); |
|
|
|
|
System.out.println("邮件大小:" + msg.getSize() * 1024 + "kb"); |
|
|
|
|
boolean isContainerAttachment = EmailParseUtils.isContainAttachment(msg); |
|
|
|
|
POP3Folder inbox = (POP3Folder) folder; |
|
|
|
|
String uid = inbox.getUID(msg); |
|
|
|
|
System.out.println("uid: ------------------" + uid); |
|
|
|
|
Flags flags = msg.getFlags(); |
|
|
|
|
if (!flags.contains(Flags.Flag.SEEN)) { |
|
|
|
|
//设置为已读
|
|
|
|
|
msg.setFlag(Flags.Flag.SEEN, true); |
|
|
|
|
} |
|
|
|
|
System.out.println(""); |
|
|
|
|
} |
|
|
|
|
System.out.println(""); |
|
|
|
|
} |
|
|
|
|
System.out.println(""); |
|
|
|
|
Date endTime = new Date(); |
|
|
|
|
//计算耗时时间
|
|
|
|
|
Date endTime = new Date(); |
|
|
|
|
//计算耗时时间
|
|
|
|
|
// double elapsedTime = CalendarUtil.arithDateTime(endTime, currentTime);
|
|
|
|
|
// 释放资源
|
|
|
|
|
folder.close(false); // false为不更新邮件,true为更新,一般在删除邮件后使用
|
|
|
|
|
store.close(); |
|
|
|
|
// 释放资源
|
|
|
|
|
folder.close(false); // false为不更新邮件,true为更新,一般在删除邮件后使用
|
|
|
|
|
store.close(); |
|
|
|
|
} catch (NoSuchProviderException e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} catch (MessagingException e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|