ESB: Extract email attachment with cProcessor and CAMEL code

Six Stars

ESB: Extract email attachment with cProcessor and CAMEL code

Hi there,

My scenario is the following:

Fetch email on POP3 every 10 seconds. If email, split into separate Message for every attachment. Save attachment locally.

 

I'm using the following recommendation from CAMEL: 

Consuming mails with attachment sample

In this sample we poll a mailbox and store all attachments from the mails as files. First, we define a route to poll the mailbox. As this sample is based on google mail, it uses the same route as shown in the SSL sample:

from("imaps://imap.gmail.com?username=YOUR_USERNAME@gmail.com&password=YOUR_PASSWORD" + "&delete=false&unseen=true&consumer.delay=60000").process(new MyMailProcessor());

Instead of logging the mail we use a processor where we can process the mail from java code:

public void process(Exchange exchange) throws Exception { // the API is a bit clunky so we need to loop Map<String, DataHandler> attachments = exchange.getIn().getAttachments(); if (attachments.size() > 0) { for (String name : attachments.keySet()) { DataHandler dh = attachments.get(name); // get the file name String filename = dh.getName(); // get the content and convert it to byte[] byte[] data = exchange.getContext().getTypeConverter() .convertTo(byte[].class, dh.getInputStream()); // write the data to a file FileOutputStream out = new FileOutputStream(filename); out.write(data); out.flush(); out.close(); } } }

As you can see the API to handle attachments is a bit clunky but it's there so you can get the javax.activation.DataHandler so you can handle the attachments using standard API.

 

How to split a mail message with attachments

In this example we consume mail messages which may have a number of attachments. What we want to do is to use the Splitter EIP per individual attachment, to process the attachments separately. For example if the mail message has 5 attachments, we want the Splitter to process five messages, each having a single attachment. To do this we need to provide a custom Expression to the Splitter where we provide a List<Message> that contains the five messages with the single attachment.

The code is provided out of the box in Camel 2.10 onwards in the camel-mail component. The code is in the class: org.apache.camel.component.mail.SplitAttachmentsExpression, which you can find the source code here

In the Camel route you then need to use this Expression in the route as shown below:{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSplitAttachmentsTest.java}If you use XML DSL then you need to declare a method call expression in the Splitter as shown below

xml<split> <method beanType="org.apache.camel.component.mail.SplitAttachmentsExpression"/> <to uri="mock:split"/> </split>

 

From Camel 2.16 onwards you can also split the attachments as byte[] to be stored as the message body. This is done by creating the expression with boolean true

SplitAttachmentsExpression split = SplitAttachmentsExpression(true);

And then use the expression with the splitter eip.

 

Putting the code in my cProcessor make my route unusable (Java Error). Does anyone already do this on his system? I don't want to make a job, really want a route.

 

Thanks for your help,

-Jacob


Accepted Solutions
Highlighted
Six Stars

Re: ESB: Extract email attachment with cProcessor and CAMEL code

Some times have passed since I've open the case. 

 

I've made a new route with same configuration and it works well.

 

Regards,

-Jacob

View solution in original post


All Replies
Highlighted
Moderator

Re: ESB: Extract email attachment with cProcessor and CAMEL code

Hello Jacob,

Could you please let us kow what's java error you are getting?

Best regards

Sabrina

 

--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Highlighted
Six Stars

Re: ESB: Extract email attachment with cProcessor and CAMEL code

Some times have passed since I've open the case. 

 

I've made a new route with same configuration and it works well.

 

Regards,

-Jacob

View solution in original post

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

Talend API Designer – Technical Overview

Take a look at this technical overview video of Talend API Designer

Watch Now

Getting Started with APIs

Find out how to get started with APIs

Read