here is my code for extracting attachments  - thank you - I also use this
with CSV files and it works ok. 

public class EmailProcessor implements Processor {

        private static final Logger LOG = 
Logger.getLogger(EmailProcessor.class);

        private SimpleDateFormat sdf = new 
SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");

        public void process(Exchange exchange) throws Exception {

                LOG.debug("Entering EmailProcessor...");

                Map<String, DataHandler> attachments = exchange.getIn()
                                .getAttachments();

                if ((null == attachments) || (attachments.size() < 1)) {
                        throw new EmailProcessorException("Null or 0 
attachements");
                } else {
                        LOG.debug("attachments.size = " + attachments.size());
                }

                Map<String, String> emailAttr = 
gatherEmailHeaderInformation(exchange);

                List<String> attachmentFilenames = new ArrayList<String>();

                try {

                        List<FilenameAndContents> attachmentArray = new
ArrayList<FilenameAndContents>();

                        for (String name : attachments.keySet()) {

                                DataHandler dh = attachments.get(name);

                                // get the file name
                                String filename = dh.getName();

                                // convert the input string to a string
                                String contents = 
exchange.getContext().getTypeConverter()
                                                .convertTo(String.class, 
dh.getInputStream());

                                LOG.info("filename: " + filename);

                                attachmentFilenames.add(filename);

                                FilenameAndContents attachmentFile = new 
FilenameAndContents();
                                attachmentFile.setFileContents(contents);
                                attachmentFile.setFileName(filename);
                                attachmentArray.add(attachmentFile);

                        }

                        exchange.getIn().setBody(attachmentArray);

                } catch (org.apache.camel.TypeConversionException tce) {
                        throw new EmailProcessorException(
                                        "Unable to type convert from file to 
string", tce);
                } catch (java.io.IOException ioe) {
                        throw new EmailProcessorException(
                                        "IOException while obtaining Input 
Stream", ioe);
                } catch (java.lang.UnsupportedOperationException uoe) {
                        throw new EmailProcessorException(
                                        "UnsupportedOperationException add 
operation is not supported by list",
                                        uoe);
                } catch (java.lang.ClassCastException cce) {
                        throw new EmailProcessorException(
                                        "ClassCastException element prevents it 
from being added to list",
                                        cce);
                } catch (java.lang.NullPointerException npe) {
                        throw new EmailProcessorException(
                                        "NullPointerException element is null", 
npe);
                } catch (java.lang.IllegalArgumentException iae) {
                        throw new EmailProcessorException(
                                        "IllegalArgumentException property of 
element prevents it from being
added to list",
                                        iae);
                }

                archiveEmail(emailAttr, attachmentFilenames, exchange);

                LOG.debug("Exiting EmailProcessor.");
        }

        private Map<String, String> gatherEmailHeaderInformation(Exchange 
exchange)
{

                final String emailBody = exchange.getIn().getBody(String.class);

                final Message mailMessage = exchange.getIn().getBody(
                                javax.mail.Message.class);

                Map<String, String> attr = new HashMap<String, String>();

                try {
                        if (null != mailMessage) {

                                final Address[] fromArray = 
mailMessage.getFrom();
                                if (null != fromArray) {
                                        String fromStr = 
convertAddressListToString(fromArray);
                                        attr.put("from", fromStr);
                                }

                                final Address[] toArray = mailMessage
                                                
.getRecipients(javax.mail.Message.RecipientType.TO);
                                if (null != toArray) {
                                        String toStr = 
convertAddressListToString(fromArray);
                                        attr.put("to", toStr);
                                }

                                final Address[] ccArray = mailMessage
                                                
.getRecipients(javax.mail.Message.RecipientType.CC);
                                if (null != ccArray) {
                                        String ccStr = 
convertAddressListToString(fromArray);
                                        attr.put("CC", ccStr);
                                }

                                final Address[] bccArray = mailMessage
                                                
.getRecipients(javax.mail.Message.RecipientType.BCC);
                                if (null != bccArray) {
                                        String bccStr = 
convertAddressListToString(fromArray);
                                        attr.put("BCC", bccStr);
                                }

                                final String subjectStr = 
mailMessage.getSubject();
                                if (null != subjectStr) {
                                        attr.put("subject", subjectStr);
                                }

                                final Date sentDate = 
mailMessage.getReceivedDate();
                                if (null != sentDate) {
                                        attr.put("sentDate", 
sdf.format(sentDate));
                                }

                                final Date receivedDate = 
mailMessage.getSentDate();
                                if (null != receivedDate) {
                                        attr.put("receivedDate", 
sdf.format(receivedDate));
                                }

                                if (null != emailBody) {
                                        attr.put("body", emailBody);
                                }

                        }
                } catch (javax.mail.MessagingException me) {
                        LOG.error("Unable to gather email header information");
                }

                return attr;

        }

        private void archiveEmail(Map<String, String> attr,
                        List<String> attachmentFilenames, Exchange exchange) {

                final String archivePath = 
exchange.getIn().getHeader("emailArchive",
                                String.class);

                Path parentP = Paths.get(archivePath);
                Path fileP;
                if (null != attr.get("receivedDate")) {
                        fileP = Paths.get(archivePath, exchange.getExchangeId() 
+ "."
                                        + attr.get("receivedDate"));
                } else {
                        fileP = Paths.get(archivePath, 
exchange.getExchangeId());
                }

                try {
                        Files.createDirectories(parentP);
                } catch (IOException ioe) {
                        LOG.error("Unable to create email archive directories");
                        ioe.printStackTrace();
                }

                Charset charset = Charset.forName("utf-8");

                try (BufferedWriter bufferedWriter = 
Files.newBufferedWriter(fileP,
                                charset)) {

                        if (null != attr.get("from")) {
                                bufferedWriter.write("From:" + 
attr.get("from"));
                                bufferedWriter.newLine();
                        }

                        if (null != attr.get("to")) {
                                bufferedWriter.write("To:" + attr.get("to"));
                                bufferedWriter.newLine();
                        }

                        if (null != attr.get("CC")) {
                                bufferedWriter.write("CC:" + attr.get("CC"));
                                bufferedWriter.newLine();
                        }

                        if (null != attr.get("BCC")) {
                                bufferedWriter.write("BCC" + attr.get("BCC"));
                                bufferedWriter.newLine();
                        }

                        if (null != attr.get("subject")) {
                                bufferedWriter.write("Subject:" + 
attr.get("subject"));
                                bufferedWriter.newLine();
                        }

                        if (null != attr.get("sentDate")) {
                                bufferedWriter.write("Sent Date:" + 
attr.get("sentDate"));
                                bufferedWriter.newLine();
                        }

                        if (null != attr.get("receivedDate")) {
                                bufferedWriter.write("Received Date:"
                                                + attr.get("receivedDate"));
                                bufferedWriter.newLine();
                        }

                        if (null != attr.get("body")) {
                                bufferedWriter.write("Body:" + 
attr.get("body"));
                                bufferedWriter.newLine();
                        }

                        for (String s : attachmentFilenames) {
                                bufferedWriter.write("Attachment File:" + s);
                                bufferedWriter.newLine();
                        }

                } catch (IOException ioe) {
                        LOG.error("Unable to write email archive");
                        ioe.printStackTrace();
                }

        }

        private String convertAddressListToString(Address[] adds) {

                String returnStr = "";

                for (Address adr : adds) {
                        returnStr = returnStr + adr.toString() + " ";
                }

                return returnStr;

        }
}




-----
Gary Lee Mills
--
View this message in context: 
http://camel.465427.n5.nabble.com/JAXB-Marshalling-from-email-route-tp5797029p5797032.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to