How to read multiple attachments with same file name

803 Views Asked by At

I have a Java Maven project and I use org.apache.camel to obtain mail and attachment information.

<dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-mail</artifactId>
        <version>2.17.0</version>
</dependency>

Given an Exchange object I use this code to get the attachments:

Message message = exchange.getIn().copy();
message.getAttachments()

Where messageCopy.getAttachments() returns a Map<String, DataHandler> that maps attachment-file-Name to DataHandler.

This code works when the mail has a single attachment or attachments that are named differently. When I have two attachments with the same name it follows that, due to the map structure, only one is returned (the other is simply overwritten).

Does anyone have the same problem and/or know another method to get two (or more) homonymous attachments?

Thanks

1

There are 1 best solutions below

0
On

I remembered to have faced this problem in my previous project. I think the workaround was to split the original message into N separate messages so that you can handle each of them, even if having the same name.

Take a look at the Camel SplitAttachmentsExpression. An existing unit test can be found here.