Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring receive emails without xml (using annotations only)

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!

like image 448
invis Avatar asked May 18 '26 19:05

invis


1 Answers

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.

like image 155
Artem Bilan Avatar answered May 22 '26 01:05

Artem Bilan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!