java 收件箱_JavaMail读取收件箱退信邮件/分析邮件附件获取Message_Id

1 packagecom.yinghuo.yingxinxin.notification.service;2

3 importcom.yinghuo.yingxinxin.notification.domain.PayrollNotificationEntity;4 importcom.yinghuo.yingxinxin.notification.domain.valobj.EmailNotificationStatus;5 importcom.yinghuo.yingxinxin.notification.repository.NotificationRepository;6 importcom.yinghuo.yingxinxin.notification.util.DateUtil;7 importcom.yinghuo.yingxinxin.notification.util.EmailUtil;8 importcom.yinghuo.yingxinxin.notification.util.StringUtil;9 importlombok.Data;10 importlombok.extern.slf4j.Slf4j;11 importorg.apache.commons.lang.exception.ExceptionUtils;12 importorg.springframework.boot.context.properties.ConfigurationProperties;13 importorg.springframework.stereotype.Service;14 importorg.springframework.transaction.annotation.Transactional;15

16 import javax.mail.*;17 importjavax.mail.search.AndTerm;18 importjavax.mail.search.ComparisonTerm;19 importjavax.mail.search.SearchTerm;20 importjavax.mail.search.SentDateTerm;21 importjavax.mail.search.SubjectTerm;22 importjava.util.Arrays;23 importjava.util.Calendar;24 importjava.util.Date;25 importjava.util.Properties;26

27 @Service28 @Slf4j29 @Data30 @ConfigurationProperties(prefix = "spring.mail")31 public classEmailBounceScanService {32 private final static String subjectKeyword = "来自postmaster@net.cn的退信";33

34 privateString popHost;35 privateString username;36 privateString password;37 privateInteger timeOffset;38 private finalNotificationRepository payrollSendRecordRepository;39

40 privateProperties buildInboxProperties() {41 Properties properties = newProperties();42 properties.setProperty("mail.store.protocol", "pop3");43 properties.setProperty("mail.pop3.host", popHost);44 properties.setProperty("mail.pop3.auth", "true");45 properties.setProperty("mail.pop3.default-encoding", "UTF-8");46 returnproperties;47 }48

49 public voidsearchInboxEmail() {50 Session session = Session.getInstance(this.buildInboxProperties());51 Store store = null;52 Folder receiveFolder = null;53 try{54 store = session.getStore("pop3");55 store.connect(username, password);56 receiveFolder = store.getFolder("inbox");57 receiveFolder.open(Folder.READ_ONLY);58

59 int messageCount =receiveFolder.getMessageCount();60 if (messageCount > 0) {61 Date now =Calendar.getInstance().getTime();62 Date timeOffsetAgo =DateUtil.nextXMinute(now, timeOffset);63 SearchTerm comparisonTermGe = newSentDateTerm(ComparisonTerm.GE, timeOffsetAgo);64 SearchTerm search = new AndTerm(newSubjectTerm(subjectKeyword), comparisonTermGe);65

66 Message[] messages =receiveFolder.search(search);67 if (messages.length == 0) {68 log.info("No bounce email was found.");69 return;70 }71 this.messageHandler(messages);72 }73 } catch(MessagingException e) {74 log.error("Exception in searchInboxEmail {}", ExceptionUtils.getFullStackTrace(e));75 e.printStackTrace();76 } finally{77 try{78 if (receiveFolder != null) {79 receiveFolder.close(true);80 }81 if (store != null) {82 store.close();83 }84 } catch(MessagingException e) {85 log.error("Exception in searchInboxEmail {}", ExceptionUtils.getFullStackTrace(e));86 e.printStackTrace();87 }88 }89 }90

91 @Transactional92 public voidmessageHandler(Message[] messageArray) {93 Arrays.stream(messageArray).filter(EmailUtil::isContainAttachment).forEach((message ->{94 String messageId = null;95 try{96 messageId =EmailUtil.getMessageId(message);97 } catch(Exception e) {98 log.error("getMessageId:", ExceptionUtils.getFullStackTrace(e));99 e.printStackTrace();100 }101 if (StringUtil.isEmpty(messageId)) return;102

103 PayrollNotificationEntity payrollNotificationEntity =payrollSendRecordRepository.findFirstByMessageId(messageId);104 if (payrollNotificationEntity == null || EmailNotificationStatus.BOUNCE.getStatus() ==payrollNotificationEntity.getStatus()) {105 log.warn("not found payrollNotificationEntity by messageId:{}", messageId);106 return;107 }108

109 payrollNotificationEntity.setStatus(EmailNotificationStatus.BOUNCE.getStatus());110 payrollNotificationEntity.setErrorMessage(EmailNotificationStatus.BOUNCE.getErrorMessage());111 payrollSendRecordRepository.save(payrollNotificationEntity);112 }));113 }114 }

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页