小编典典

Spring收到不带xml的电子邮件(仅使用注释)

spring-boot

我需要定期检查大约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的新手,因此对您的帮助非常感谢!


阅读 277

收藏
2020-05-30

共1个答案

小编典典

您正在寻找正确的方法- @InboundChannelAdapter。如果您正确看一下“
文档”,将会看到类似以下内容:

@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;
}

所以与其他@BeanS表示MessageChannel@ServiceActivatorEmailReceiverService

Spring Integration Java DSL视为Java配置工具。

2020-05-30