baao
baao

Reputation: 73271

how to correctly get inbound message headers with spring integration

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?

Upvotes: 1

Views: 2405

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121552

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.

Upvotes: 1

Related Questions