spring 接收没有 xml 的电子邮件(仅使用注释)

spring receive emails without xml (using annotations only)

我需要定期检查大约 30 个邮箱,并且只想使用批注来完成此操作。我知道如何处理 XML 个文件,它看起来像这样:

<mail:inbound-channel-adapter id="ImapAdapter"
                              store-uri="imaps://${login}:${pass}@${host}:993/inbox"
                              channel="testReceiveEmailChannel"
                              should-delete-messages="false"
                              should-mark-messages-as-read="true"
                              auto-startup="true"
                              java-mail-properties="javaMailProperties">
    <int:poller fixed-delay="200"
                time-unit="SECONDS"
                task-executor="asyncTaskExecutor"/>
</mail:inbound-channel-adapter>

<int:channel id="testReceiveEmailChannel">
    <int:interceptors>
        <int:wire-tap channel="logger"/>
    </int:interceptors>
</int:channel>

<int:service-activator input-channel="testReceiveEmailChannel"
                       ref="testMailReceiverService"
                       method="receive"/>

<bean id="testMailReceiverService" class="com.myproject.email.EmailReceiverService">
    <property name="mailBox" value="${login}"/>
</bean>


<int:logging-channel-adapter id="logger" level="DEBUG"/>

我知道 Spring 4+ 有 @InboundChannelAdapter 但我不知道如何使用它。实际上我是 Spring 的新人,所以非常感谢任何帮助!

您正在寻找正确的方法 - @InboundChannelAdapter。如果您正确查看 Documentation,您会看到类似这样的内容:

@Bean
@InboundChannelAdapter(value = "testReceiveEmailChannel", poller = @Poller(fixedDelay = "200000", taskExecutor = "asyncTaskExecutor"))
public MessageSource<javax.mail.Message> mailMessageSource(MailReceiver mailReceiver) {
    MailReceivingMessageSource mailReceivingMessageSource = new MailReceivingMessageSource(mailReceiver);
    // other setters here
    return mailReceivingMessageSource;
}

其中 MailReceiver 是这样的:

@Bean
public MailReceiver imapMailReceiver(@Value("imaps://${login}:${pass}@${host}:993/inbox") storeUrl) {
     ImapMailReceiver imapMailReceiver = new ImapMailReceiver(storeUrl);
        // other setters here
     return imapMailReceiver;
}

等等 MessageChannel 的其他 @BeanEmailReceiverService@ServiceActivator

考虑将 Java 配置为 Spring Integration Java DSL