javaapache-camelapache-camel-mail

Get mail attachment information with Apache Camel


How can I get mail attachment information with Apache Camel, having an Exchange object already?


Solution

  • If you have a maven project, you can use this dependency:

    <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-mail</artifactId>
            <version>x.y.z</version>
    </dependency>
    

    and then if you already have an Exchange object, you can try this:

    Message message = exchange.getIn();
        if (message.getAttachments().size() > 0) {
            for (Map.Entry<String, DataHandler> entry : messageCopy.getAttachments().entrySet()) {
                DataHandler dataHandler = entry.getValue();
                //get the document filename                
                String attchmentFilename = dataHandler.getName();
                String decodedFilename = MimeUtility.decodeText(attchmentFilename);
                //get inputstream of attachment file
                InputStream is = dataHandler.getInputStream();              
                //generic function for extracting attachment content
                extractor(is);
            }
        }
    

    I suggest to use Apache Tika inside extractor function for obtaining attachment content and other information