简体   繁体   中英

Receive Mail with Spring Integration?

I have a simple project in spring integration that is attempting to receive mail:

@Service
public class MailReceiverService implements CommandLineRunner {

    @Bean
    TestMailServer.ImapServer imapServer() {
        return TestMailServer.imap(0);
    }

    private static Log logger = LogFactory.getLog(MailReceiverService.class);

    public static void manageMessage() {

    @SuppressWarnings("resource")
    ClassPathXmlApplicationContext ac =
        new ClassPathXmlApplicationContext(
            "/integration/gmail-imap-idle-config.xml");
    DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);


            inputChannel.subscribe(message -> {
                logger.info("Message: " + message);
            });



  }

    @Override
    public void run(String... args) {
        manageMessage();
    }


}

The gmail-imap-idle-config.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/integration/mail https://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"
    xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
    xmlns:util="http://www.springframework.org/schema/util">

    <int:channel id="receiveChannel" />
    <int-mail:imap-idle-channel-adapter id="customAdapter"
            store-uri="imaps://username:password@imap.gmail.com:993/inbox"
            channel="receiveChannel"
            auto-startup="true"
            should-delete-messages="false"
            should-mark-messages-as-read="false"
            java-mail-properties="javaMailProperties"/>

    <util:properties id="javaMailProperties">
        <prop key="mail.imap.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
        <prop key="mail.imap.socketFactory.fallback">false</prop>
        <prop key="mail.store.protocol">imaps</prop>
        <prop key="mail.debug">false</prop>
    </util:properties>

</beans>

However when I run it I get this message:

Channel 'application.errorChannel' has 0 subscriber(s). stopped bean '_org.springframework.integration.errorLogger'

And it shuts down! any suggestions on how to get this working? Thanks!

Your problem that you don't keep an application alive. The <int-mail:imap-idle-channel-adapter> initiates a long-running process on the background. But when you start your application, you step immediately away from the main thread and that one just exits. In the meantime you get a message from email box, but a subscriber for the receiveChannel is gone already because your application in a closing state according a main thread behavior.

Consider to have some barrier in that run() do not the main thread to exit.

In samples we typically have something like this:

System.out.println("Hit 'Enter' to terminate");
System.in.read();
ctx.close();

This way you won't let a JVM to exit and therefore will receive emails until you terminate the application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM