Egor
Egor

Reputation: 2192

How to avoid loading email's content using spring integration mail

I am using spring integration mail (5.3.1 release) I have a flow:

    IntegrationFlows
    .from(Mail
        .imapIdleAdapter(imapAdapter)
    )
    .filter()
    .filter()
    ...
    .filter()
    .handle(service1)
    .get();

I want an email content to be loaded in service1. I don't want to load email's content until it passes all filters. My filters need to know only email headers.

I tried to use DefaultMailHeaderMapper but email's content is loaded anyway. I can see it in logs using "mail.debug"=true.

I was debugging and according to source of AbstractMailReceiver#receive, MimeMessage's content will be always loaded because e.g new IntegrationMimeMessage() uses MimeMessage(MimeMessage message) constructor that loads a content.

Is there any way to configure mail adapter to not load an email's content?

Thank you!

Upvotes: 1

Views: 258

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121282

See docs: https://docs.spring.io/spring-integration/docs/current/reference/html/mail.html#mail-inbound. Especially this part:

tarting with version 5.2, the autoCloseFolder option is provided on the mail receiver. Setting it to false doesn’t close the folder automatically after a fetch, but instead an IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE header (see MessageHeaderAccessor API for more information) is populated into every message to producer from the channel adapter.

And then take a look into the next section: https://docs.spring.io/spring-integration/docs/current/reference/html/mail.html#mail-mapping

So, to avoid eager content loading you should abandon the header mapper and don't close the folder automatically. This way the whole MimeMessage is sent as a payload. You probably won't be able to perform your filtering logic against headers because the content of the message is not fetched therefore we don't know what headers are there in MimeMessage. However you can try to get access to them from your filters, but already against the payload - not headers.

Upvotes: 2

Related Questions