当前位置: 首页 > 知识库问答 >
问题:

Spring集成-imap邮件接收器

龚铭
2023-03-14

我使用的是spring Boot2.2.4-Release和spring integration 5.2.3,我使用的是IntegrationFlow和DSL,因为我需要配置几个IMAP服务器。

所以我写了这段代码:

String flowId = MAIL_IN_FLOW_ID_PREFIX+cpd.getIndirizzoMail();
if( flowContext.getRegistrationById(flowId) != null ) {
    flowContext.remove(flowId);
}
ImapIdleChannelAdapterSpec imapIdleChannelAdapterSpec = Mail.imapIdleAdapter(connectionUrl.toString())
    .javaMailProperties(javaMailProperties)
    .shouldDeleteMessages(deleteMessages)
    .shouldMarkMessagesAsRead(markMessagesRead)
    .autoStartup(true)
    .autoCloseFolder(false)
    .id(confMailIn.getHost()+"_adapter")
    .selector(selectFunction);
IntegrationFlow flowIdle = IntegrationFlows.from(imapIdleChannelAdapterSpec)
    .handle(msgHandler)
    .get();
flowContext.registration(flowIdle).id(flowId).register();

其中msghandler

@Component
public class MailMessageHandler implements MessageHandler {
    private static final Logger logger = LoggerFactory.getLogger(MailMessageHandler.class.getName());
    @Autowired
    private IConfigCasPostaleSvc ccps;
    @Autowired
    private IGestioneMailSvc gestioneMailSvc;
    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        MimeMessage mimeMessage = (MimeMessage) message.getPayload();
        if( logger.isDebugEnabled() ) {

            try {
                mimeMessage.getAllHeaders().asIterator().forEachRemaining(header->{
                    logger.debug("Header name {} header value {}", header.getName(), header.getValue());
                });
            } catch (javax.mail.MessagingException e) {
                logger.error("Errore nella lettura degli header", e);
            }
        }
        try {
            //Recupero i dati del messaggio
            MimeMessageParser parser = new MimeMessageParser(mimeMessage);
            parser = parser.parse();
            String mailId = mimeMessage.getMessageID();
            String oggettoMail = parser.getSubject();
            Date receivedDate = mimeMessage.getReceivedDate();
            List<DataSource> allegatiMail = parser.getAttachmentList();

            String corpoMail = parser.getHtmlContent();
            if( !StringUtils.hasText(corpoMail) ) {
                if( logger.isDebugEnabled() ) {
                    logger.debug("Nessun contenuto HTML nella mail; recupero il contenuto plain/text");
                }
                corpoMail = parser.getPlainContent();
            }
            MailInDto datiMail = new MailInDto();
            datiMail.setAllegatiMail(allegatiMail);
            datiMail.setIdMail(mailId);
            datiMail.setOggettoMail(oggettoMail);
            datiMail.setDataRicezioneMail(receivedDate);
            datiMail.setAllegatiMail(allegatiMail);
            datiMail.setCorpoMail(corpoMail);
            List<Address> destinatari = parser.getTo();
            for (Address to:destinatari) {

                String destinatario = to.toString();
                if( to instanceof InternetAddress ){
                    destinatario = ((InternetAddress)to).getAddress();
                }
                //Considero solo i destinatari che sono censiti nelle nostre tabelle
                Optional<ConfigurazioneCasellaPostaleDto> configurazioneCasellaPostale = ccps.getConfigurazioneCasellaPostale(destinatario);
                if( configurazioneCasellaPostale.isPresent() ) {
                    ConfigurazioneCasellaPostaleDto ccpd = configurazioneCasellaPostale.get();
                    //Indico l'UUID del documentale che contiene tutti i messaggi mail della casella postale
                    datiMail.setParentIdFolder(ccpd.getCasellaPostale().getIdFolderDocumentale());
                    //Posso salvare
                    datiMail.setIdCasellaPostale(ccpd.getCasellaPostale().getPk());
                    this.gestioneMailSvc.storeReceivedMail(datiMail, parser);
                }else {
                    if( logger.isDebugEnabled() ) {
                        logger.debug("Nessuna configurazione casella postale trovata per il destinatario {}", destinatario);
                    }
                }
            }
        }catch (Exception e) {

            throw new MessagingException("Errore nella gestione del messaggio "+message, e); 
        }
    }
}

通过设置.AutoCloseFolder(false),我可以在处理程序组件中处理MimeMessage,但我有一些问题。

    null

我不知道是否应该做什么来告诉框架关闭文件夹

谁能给我提点建议吗?

谢谢你

共有1个答案

郎宏逸
2023-03-14

我认为调用FlowContext.Remove(flowId);时会得到“文件夹关闭异常”,但MailMessageHandler中仍有一些进程试图读取MimeMessage

若要手动关闭文件夹,您需要访问StaticMessageHeaderAccessor.GetCloseableResource(message)并调用其close()

 类似资料:
  • 在使用IMAP和PHP之前,我从未尝试过获取和移动电子邮件,因此将发送到垃圾箱的电子邮件移回收件箱。 好的,所以我可以正确地从Gmail中提取电子邮件,我可以删除它们,或者将它们移到垃圾箱。 当我尝试将它们移回收件箱时,我收到以下错误: 注意:未知:[TRYCREATE]第0行未知中没有文件夹[Gmail]/收件箱(失败)(errflg=2) 所以很明显,有些事情我不太明白,在过去的几个小时里,我

  • 本文向大家介绍使用c#+IMap实现收取163邮件,包括了使用c#+IMap实现收取163邮件的使用技巧和注意事项,需要的朋友参考一下 最近我要做一个爬虫。这个爬虫需要如下几个步骤: 1 填写注册内容(需要邮箱注册) 2 过拖拽验证码(geetest) 3 注册成功会给邮箱发一封确认邮箱 4 点击确认邮箱中的链接 完成注册 我这里就采用163邮箱注册。 邮箱协议有 pop3 和 imap 和 sm

  • 我正在尝试使用Java应用程序连接iCloud邮件。我想使用用户凭据从iCloud IMAP邮件服务器读取邮件。但它不起作用。下面的代码片段适用于Gmail、Yahoo和Outlook,但不适用于iCloud: 调试信息为: debug:setdebug:JavaMail版本1.5.4 调试:getProvider()返回 javax.mail.provider[STORE,imaps,com.s

  • 我正在尝试创建一个TCP服务器,该服务器在端口5002上接受来自外部程序的消息。但是,它不接收来自外部程序的消息。 为了验证我的TCP服务器是否正常工作,我像这样使用了telnet,程序确实收到了文本“hello”。 设置wireshark时,我可以看到计算机正在端口5002上接收来自外部程序(我期待)的消息。为什么我的程序无法接收这些消息? 关于最终解决方案的最新情况: 由于负载没有停止线,我必

  • 本文向大家介绍PHP基于IMAP收取邮件的方法示例,包括了PHP基于IMAP收取邮件的方法示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了PHP基于IMAP收取邮件的方法。分享给大家供大家参考,具体如下: 因需要处理公司企业邮箱被退信邮件问题:刚开始的思路是通过模拟登陆,对邮件进行抓取,(这种事情对我来说,做过了很多次,轻车熟路),不过邮箱还是有点复杂的, 后来想起做邮件发送时有用到s

  • 我是spring Integration的新手。在spring integration的配置中,我有: 在CSVEntreprise类中,我用布尔返回定义了方法,我希望当它返回true时使用通道channel1_3TRUE,当它返回false时使用通道channel1_3FALSE?