Spring 集成邮件 IMAP - 多个接收者

Spring Integration Mail IMAP - multiple receiver

我正在使用 spring 引导 2.2。4.RELEASE

我需要构建一个动态邮件接收器,因为我可以有多个邮件服务器来获取邮件。邮件服务器必须可由其他系统配置,因此我的要求是能够动态地获取消息。

我进行了调查,我喜欢 Spring 集成解决方案及其 DSL(注意:我只需下载消息及其附件(如果有的话)就足够了)。

所以我构建了这段代码:

String flowId = MAIL_IN_FLOW_ID_PREFIX+cpd.getIndirizzoMail();
if( flowContext.getRegistrationById(flowId) != null ) {
    flowContext.remove(flowId);
}
ImapMailInboundChannelAdapterSpec adapterSpec = Mail.imapInboundAdapter(connectionUrl.toString())
    .javaMailProperties(javaMailProperties)
    .shouldDeleteMessages(false)
    .shouldMarkMessagesAsRead(false)
    .selector(selectFunction);
if( confMailIn.isRichiedeAutenticazione() ) {
     adapterSpec = adapterSpec.javaMailAuthenticator(new CasellaPostaleAuthenticator(cpd.getUsername(), cpd.getPassword()));
}

IntegrationFlow flow = IntegrationFlows
.from(adapterSpec.get(), e -> e.poller(Pollers.fixedDelay(Duration.ofSeconds(pollingSeconds)).maxMessagesPerPoll(maxMailMessagePerPoll)))
.handle(message -> {

    logger.info("Message headers "+message.getHeaders());
    logger.info("Message payload "+message.getPayload());
})

.get();

flowContext.registration(flow).id(flowId).register();

我尝试使用我的 gmail 帐户。该代码能够通过 imap 连接到 GMAIL,但是,当我尝试简单地记录消息时,出现此错误:

A6 OK Success

2020-02-05 12:48:41,835 23412 [task-scheduler-1] DEBUG o.s.i.mail.ImapMailReceiver - Received 10 messages

2020-02-05 12:48:41,836 23413 [task-scheduler-1] DEBUG oA7 STORE 1 +FLAGS (\Flagged) .s.i.mail.ImapMailReceiver - USER flags are not supported by this mail server. Flagging message with system flag

A7 NO STORE attempt on READ-ONLY folder (Failure)

A8 CLOSE

A8 OK Returned to authenticated state. (Success)

DEBUG IMAP: added an Authenticated connection -- size: 1

2020-02-05 12:48:42,198 23775 [task-scheduler-1] ERROR o.s.i.handler.LoggingHandler - org.springframework.messaging.MessagingException: failure occurred while polling for mail; nested exception is javax.mail.MessagingException: A7 NO STORE attempt on READ-ONLY folder (Failure); nested exception is: com.sun.mail.iap.CommandFailedException: A7 NO STORE attempt on READ-ONLY folder (Failure) at org.springframework.integration.mail.MailReceivingMessageSource.doReceive(MailReceivingMessageSource.java:74) at org.springframework.integration.endpoint.AbstractMessageSource.receive(AbstractMessageSource.java:167) at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:250)

现在似乎默认情况下 FOLDER 以 READ_ONLY 方式打开,这似乎会导致错误。

我被困在这里,不知道如何解决这个问题。

有人可以给我提示吗?

谢谢

安杰洛

adapterSpec.get()

在规范上发布 get() 绕过 Spring 的 bean 初始化逻辑,该逻辑将文件夹切换到 read/write。

要么使适配器成为 @Bean,要么简单地删除 .get(),Spring 将执行初始化。

.from(adapterSpec, e -> ...