0

I have a spring app that sends emails through and reads emails from a GMail inbox and handles any undelivered mail messages that have been sent through the account but then returned as the recipient address is not recognised.

I have this all configured and working but I am also setting a custom header on the outbound message that I would like to retrieve when the returned mail is read. Debugging the app shows that the message is being read correctly but it is missing the custom header value.

Here is my config:

<mail:inbound-channel-adapter id="imapAdapter"
        store-uri="#{mailIntegrationProperties['mail.imap.url']}"
        java-mail-properties="mailIntegrationProperties" 
        channel="receiveEmailChannel"
        should-delete-messages="false" 
        should-mark-messages-as-read="true"
        auto-startup="true"
        max-fetch-size="#{mailIntegrationProperties['mail.max.fetch.size']}">
    <int:poller time-unit="SECONDS" fixed-rate="#{mailIntegrationProperties['mail.poller.seconds']}" />
</mail:inbound-channel-adapter>

<int:channel id="receiveEmailChannel" />

<bean id="mailMessageReceiver" class="com.myapp.integration.mail.EmailMessageReceiver" />

<int:service-activator input-channel="receiveEmailChannel" ref="mailMessageReceiver" output-channel="emailEventMessageQueue" method="receive" />

I have left the output-channel of the service-activator off for brevity but the system is working as expected, but just the custom header is missing when I am processing the message.

Any ideas on how to configure Spring to read that custom header?

Cheers!

pmckeown
  • 3,419
  • 3
  • 31
  • 35

1 Answers1

1

I assume you mean you are setting some 'X-foo' header on an SMTP outbound message, and want to see it on the message received.

The payload of the message placed on the receiveEmailChannel is the mime message received; there is no filtering of the headers and all headers received will be included in it.

If you don't see the header, it means it wasn't in the message.

If you turn on debugging <prop key="mail.debug">true</prop> in your javamail properties, you'll see the message in the console as it is received (including all the headers).

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Hey Gary, with mail.debug enabled the message is displayed in my server console with the X-foo header. Though when I debug with a breakpoint in the mailMessageReceiver bean, which is the first point of my code that interacts with Spring Integration in this workflow, the MimeMessage handed to my code doesn't have the X-foo header. On the off chance I also upgraded 2 minor version of Spring Int, from 2.2.0 to 2.2.2 but that made no difference. I'm wondering if there is some Spring config I'm missing? Ta :) – pmckeown Mar 26 '13 at 06:49
  • Further debugging has shown that the Spring AbstractMailReceiver is not setting the FetchProfile flag to retrieve all headers. Am investigating whether is a Java Mail property I can use to set enable this or possibly subclassing the ImapMailReceiver... – pmckeown Mar 26 '13 at 07:03
  • Doesn't look like this is possible. There is no way to configure the ImapMailReceiver or AbstractMailReceiver to add `IMAPFolder.FetchProfileItem.HEADERS` to the fetch profile that is passed into the JavaMail API. Do you know how I can register a custom bean to override `AbstractMailReceiver.fetchMessages()`? I'm not sure how to do this with the int-mail namespace. Also, any chance of something like configuration for this making it into Spring Integration 3? Cheers! – pmckeown Mar 26 '13 at 08:04
  • Hmmmm - that doesn't seem to be an issue for me - I just ran a test against gmail and received a message with 22 headers, including several `X-*` headers; and all 22 headers appear in the MimeMessage (breakpoint in a downstream component). Just to be clear - if you see the headers in the DEBUG log (right after `* 7 FETCH (BODY[]<0> {2724}` then thay *have* been fetched - we just have to figure out why they are not making it into your `MimeMessage`. – Gary Russell Mar 26 '13 at 12:43
  • To answer your specific question, there is not currently a way to inject a customized MailReceiver using the namespace. But, you can construct the adapter using `` syntax - you create a `MailReceivingMessageSource` that gets the `MailReceiver` in its constructor. The message source in turn is injected into a `SourcePollingChannelAdapter`, along with `PollerMetadata`. If you indeed find this solves the problem, feel free to open a JIRA issue for 3.0 to allow injection of a custom MailReceiver. – Gary Russell Mar 26 '13 at 12:47
  • Debugging further, I set a breakpoint at line 272 in `AbstractMailReceiver` (in `postProcessFilteredMessages`); stepping over that line does the actual FETCH - I see the headers in the console - and builds the `MimeMessage` object (which, in my case, contains all 22 headers). Can you try the same thing? – Gary Russell Mar 26 '13 at 12:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26948/discussion-between-gary-russell-and-evil-raat) – Gary Russell Mar 26 '13 at 13:41
  • Hi Gary, the time difference (I'm in NZ) and my work's Net Nanny may hinder a chat. However, I will try the debug steps you suggested above when I get home (this is a pet project). Out of interest, what version JDK and Spring integration are you running? – pmckeown Mar 26 '13 at 20:59
  • StackExchange/StackOverflow doesn't like looooong comment threads (see "Please avoid extended discussions in comments" below). A 'chat' in this context doesn't mean interactive - just that the discussion is taken "offline" (with a link from the "last" comment). I have tested with 2.1.x and 3.0.0.BUILD-SNAPSHOT (which is essentially 2.2.2) and JDK 6. Click on the link 2 comments ago to get to the chat. – Gary Russell Mar 26 '13 at 21:06