spring receive emails without xml (using annotations only)

陌路散爱 提交于 2019-12-05 02:33:27

问题


I need to periodically check about 30 mailboxes and want to do this with annotations only. I know how to do it with XML files, it looks like this:

<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"/>

I know that Spring 4+ have @InboundChannelAdapter but I dont know how to use it. Actually I am new in Spring, so any helps very appreciated!


回答1:


You are looking into the correct way - @InboundChannelAdapter. If you take a look to the Documentation properly, you'll see something like this:

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

Where MailReceiver is something like this:

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

and so with other @Beans for MessageChannel and @ServiceActivator for your EmailReceiverService.

Consider as a tool for Java Configuration the Spring Integration Java DSL.



来源:https://stackoverflow.com/questions/32401643/spring-receive-emails-without-xml-using-annotations-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!