从 Spring 集成邮件接收通道适配器获取实际电子邮件



如何从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 ?
            }
        });
    }
}
javax.mail.Message mailMessage = message.getPayload();

编辑

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

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

框架将为您解开它。

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

最新更新