从Spring Integration Mail-Receiving Channel Adapter获取实际的电子邮件消息

时间:2016-02-03 13:02:06

标签: java spring spring-integration

如何从org.springframework.messaging.Message

中提取实际的电子邮件

这是我的实际代码。

配置:

<int-mail:imap-idle-channel-adapter
    id="customAdapter" 
    store-uri="imaps://username:password@host/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>

爪哇:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;

public class ImapTestApp {

    public static void main(String[] args) throws Exception {

        ApplicationContext ac = new ClassPathXmlApplicationContext("/config-imap.xml");

        DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);

        inputChannel.subscribe(new MessageHandler() {

            public void handleMessage(Message<?> message) throws MessagingException {
                // and now ?
            }

        });

    }
}

1 个答案:

答案 0 :(得分:1)

javax.mail.Message mailMessage = message.getPayload();

修改

请记住,使用当前代码,适配器可能会在您订阅频道之前启动 - 最好在上下文中订阅某些内容。您甚至可以使用<service-activator/>引用POJO方法。

public void handle(javax.mail.Message message) {...}

并且框架将为您打开它。

如果您想保留当前代码,则应将自动启动设置为false并在订阅频道后启动适配器。