在Spring与Java config集成中接收邮件

use*_*545 2 java dsl spring spring-integration

我想通过Spring Integration接收邮件。我在xml配置中找到了很多示例,但在Java DSL配置中却找不到任何示例。如何使用Java DSL编写以下xml配置?

<int-mail:inbound-channel-adapter id="imapAdapter"
      store-uri="imaps://[username]:[password]@imap.gmail.com/INBOX"
      channel="receiveChannel"
      should-delete-messages="true">
      <int:poller max-messages-per-poll="1" fixed-rate="5000"/>
</int-mail:inbound-channel-adapter>
Run Code Online (Sandbox Code Playgroud)

我尝试了以下解决方案,但我不知道如何向其中添加轮询器。

@Bean
public IntegrationFlow mailListener() {
    return IntegrationFlows.from(Mail.imapInboundAdapter("imaps://[username]:[password]@imap.gmail.com/INBOX").shouldDeleteMessages(true).get())
            .<Message>handle((payload, header) -> logMail(payload))
            .get();
}
Run Code Online (Sandbox Code Playgroud)

Gar*_*ell 5

请参阅DSL参考

@Bean
public IntegrationFlow mailListener() {
    return IntegrationFlows.from(Mail.imapInboundAdapter("imaps://[username]:[password]@imap.gmail.com/INBOX")
             .shouldDeleteMessages(true).get(), 
             e -> e.poller(Pollers.fixedRate(5000).maxMessagesPerPoll(1)))
        .<Message>handle((payload, header) -> logMail(payload))
        .get();
}
Run Code Online (Sandbox Code Playgroud)

  • 不要评论旧答案;提出一个新问题以显示您的配置。显然,您的凭证是错误的;您的密码很可能具有一些需要转义的特殊字符。 (2认同)