how to correctly get inbound message headers with spring integration

点点圈 提交于 2019-12-11 06:16:51

问题


I'm receiving email with spring, with a very basic script so far

ApplicationContext ac = new ClassPathXmlApplicationContext("imap.xml");
DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);
inputChannel.subscribe(message -> {
    System.out.println(message.getHeaders());
    System.out.println(message.getPayload());

    MessageHeaders headers = message.getHeaders();
    String from = (String) headers.get("mail_from");
});

According to the documentation I thought the headers would get parsed automatically, but the headers I get with the first System.out.println(); are just

{id=c65f55aa-c611-71ee-c56d-6bf13c6f71d0, timestamp=1468869891279}

The second output (for getPayload()) is

org.springframework.integration.mail.AbstractMailReceiver$IntegrationMimeMessage@6af5615d

from outputs null...

I then tried to use the MailToStringTransformer

MailToStringTransformer a = transformer();
    a.setCharset("utf-8");
System.out.println(a.transform(message));

Which outputs the payload and all the headers I have expected, but (naturally) as a String.

What do I have to do to get the messages headers and text in an object?


回答1:


Not sure which documentation are you referring, but the current 4.3 version has this:

By default, the payload of messages produced by the inbound adapters is the raw MimeMessage; you can interrogate the headers and content using that object. Starting with version 4.3, you can provide a HeaderMapper<MimeMessage> to map the headers to MessageHeaders; for convenience, a DefaultMailHeaderMapper is provided for this purpose.

And a bit below:

When you do not provide a header mapper, the message payload is the MimeMessage presented by javax.mail. The framework provides a MailToStringTransformer...

If you need some customization on the mapping you always can provide your own HeaderMapper<MimeMessage> implementation or DefaultMailHeaderMapper extension.



来源:https://stackoverflow.com/questions/38444803/how-to-correctly-get-inbound-message-headers-with-spring-integration

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