Nicomedes E. Nicomedes E. -4 years ago 262
Java Question

Get mail attachment information with Apache Camel

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

Answer Source

If you have a maven project, you can use this 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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download