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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值