You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by LNB <ln...@kluwer.nl> on 2012/01/06 11:32:08 UTC

How to send a templated mail with attachment?

In my camel route I download and unpack zip files. When something goes wrong,
I want to send a mail with the zip file that has gone wrong as an
attachment. I want to construct the body of this mail using a template. 

When I first add the zip file as an attachment to the message and next route
it to a template, the attachment is lost. When I first route the message to
the template, I lose the zipfile as message body and can't add it as
attachment.

Is it possible to use a template with an attachment?

--
View this message in context: http://camel.465427.n5.nabble.com/How-to-send-a-templated-mail-with-attachment-tp5125167p5125167.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to send a templated mail with attachment?

Posted by Christian Müller <ch...@gmail.com>.
We just released Camel 2.9.0 2 weeks ago.
We try to build a new minor version (2.10.0) after round about 3 month. You
will see Camel 2.9.1 earlier, but it will also takes some more weeks.

Best,
Christian

On Mon, Jan 9, 2012 at 2:58 PM, v_peter <v_...@ukr.net> wrote:

> Hi,
>
> Do you have any information about plans of 2.9.1 and/or 2.10.0 releases?
>
> Regards,
> Peter Velychko
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-send-a-templated-mail-with-attachment-tp5125167p5131386.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: How to send a templated mail with attachment?

Posted by v_peter <v_...@ukr.net>.
Hi, 

Do you have any information about plans of 2.9.1 and/or 2.10.0 releases?

Regards,
Peter Velychko

--
View this message in context: http://camel.465427.n5.nabble.com/How-to-send-a-templated-mail-with-attachment-tp5125167p5131386.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to send a templated mail with attachment?

Posted by Christian Müller <ch...@gmail.com>.
Which Camel version do you use?

How the stack trace looks like?

Do you consider to provide a unit test to show the issue? Than it's easier
for us to find/fix the issue.

Best,

Christian
Am 11.01.2013 09:40 schrieb "Arjen Duursma" <du...@zorgdomein.nl>:

> The same (missing attachments) seems to be happening to other components as
> well.
>
> I have a route retrieving reports on failed mail attempts
> (multipart/report)
> from a pop3 component. Using a xslt transform for generating the actual
> body
> before remailing it.
>
> After 'saving' the relevant information as an attachement (tested and
> works)
> I try to enrich the message based on some identifier from the subject of
> the
> original mail.  I do this by including:
> <route>
>   <from direct:extra-info />
>   <setBody><xquery ... ></setBody>
>   <to uri:jdbc:... />
>   <marshal><xstream encoding="UTF-8" /></marshal>
> </route>
>
> After this route, the attachements are gone :-(
>
> I modified the route to call this route using the 'enrich' pattern using a
> generic "ReplaceBodyAggregation".
> This fails due to a NullPointerException in the XmlConverter during the
> xslt
> rendering of the replaced body.  The system doesn't seem to be able to find
> typeconverters in the CamelContext.  If I make the ReplaceBodyAggregation
> less generic by specifying the expected body class as "Document.class", it
> suddenly works.
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-send-a-templated-mail-with-attachment-tp5125167p5725353.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: How to send a templated mail with attachment?

Posted by Arjen Duursma <du...@zorgdomein.nl>.
The same (missing attachments) seems to be happening to other components as
well. 

I have a route retrieving reports on failed mail attempts (multipart/report)
from a pop3 component. Using a xslt transform for generating the actual body
before remailing it.  

After 'saving' the relevant information as an attachement (tested and works)
I try to enrich the message based on some identifier from the subject of the
original mail.  I do this by including:
<route>
  <from direct:extra-info />
  <setBody><xquery ... ></setBody>
  <to uri:jdbc:... />
  <marshal><xstream encoding="UTF-8" /></marshal>
</route>

After this route, the attachements are gone :-(

I modified the route to call this route using the 'enrich' pattern using a
generic "ReplaceBodyAggregation".
This fails due to a NullPointerException in the XmlConverter during the xslt
rendering of the replaced body.  The system doesn't seem to be able to find
typeconverters in the CamelContext.  If I make the ReplaceBodyAggregation
less generic by specifying the expected body class as "Document.class", it
suddenly works.



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-send-a-templated-mail-with-attachment-tp5125167p5725353.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to send a templated mail with attachment?

Posted by Christian Müller <ch...@gmail.com>.
Exactly.
I copy pasted the sniped from one of our routes which are more complex. But
as already mentioned, if you don't need any complex pre processing, your
valid route could also looks as below:
from("xxx")
  .enrich("stringtemplate:XXX", aggregationStrategie)
  .to("smtp:XXX"");

Best,
Christian

On Tue, Jan 10, 2012 at 11:55 AM, v_peter <v_...@ukr.net> wrote:

> Actually you can use "stringtemplate" or any other uri in "enrich".
> But using "direct" helps to split the process in separate logical steps.
> You
> can have "direct:generateEmailBody" and if you need to do anything else for
> the step you can simply update the route.
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-send-a-templated-mail-with-attachment-tp5125167p5133857.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: How to send a templated mail with attachment?

Posted by v_peter <v_...@ukr.net>.
Actually you can use "stringtemplate" or any other uri in "enrich". 
But using "direct" helps to split the process in separate logical steps. You
can have "direct:generateEmailBody" and if you need to do anything else for
the step you can simply update the route.

--
View this message in context: http://camel.465427.n5.nabble.com/How-to-send-a-templated-mail-with-attachment-tp5125167p5133857.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to send a templated mail with attachment?

Posted by LNB <ln...@kluwer.nl>.
Thanks for fixing, I didn't try it out because I think I cannot use a
snapshot version. But I used enrich as you proposed so now I can send emails
with an attachment :). Is there a reason to use a "direct" endpoint in
between, instead of using the stringtemplate endpoint directly in the enrich
method?



Christian Mueller wrote
> 
> I had to fix camel-stringtemplate, camel-freemaker and camel-velocity in
> all supported branches (trunk, camel-2.9.x, camel-2.8.x and camel-2.7.x).
> If you cannot use a snapshot version (what I asume), I would propose to
> use
> the enricher component as a workaround. Something like:
> 
> from("xxx")
>   .enrich("direct:workaround", aggregationStrategie)
>   .to("smtp:XXX"");
> 
> from("direct:workaround")
>   .to("stringtemplate:XXX");
> 
> And the aggregation strategie can copy the attachment from the old
> exchange
> to the new one.
> 
> Sorry for this inconvenience. Let me know if you need help here.
> 
> Best,
> Christian
> 


--
View this message in context: http://camel.465427.n5.nabble.com/How-to-send-a-templated-mail-with-attachment-tp5125167p5133615.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to send a templated mail with attachment?

Posted by Christian Müller <ch...@gmail.com>.
I had to fix camel-stringtemplate, camel-freemaker and camel-velocity in
all supported branches (trunk, camel-2.9.x, camel-2.8.x and camel-2.7.x).
If you cannot use a snapshot version (what I asume), I would propose to use
the enricher component as a workaround. Something like:

from("xxx")
  .enrich("direct:workaround", aggregationStrategie)
  .to("smtp:XXX"");

from("direct:workaround")
  .to("stringtemplate:XXX");

And the aggregation strategie can copy the attachment from the old exchange
to the new one.

Sorry for this inconvenience. Let me know if you need help here.

Best,
Christian

Re: How to send a templated mail with attachment?

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Thanks for reporting.

Attachments is seldom used with Camel. I have created a ticket to track this bug
https://issues.apache.org/jira/browse/CAMEL-4870



On Fri, Jan 6, 2012 at 4:01 PM, LNB <ln...@kluwer.nl> wrote:
> I'm using camel 2.7.3 and a stringtemplate, but I could use an other template
> if that would be useful.
> My code looks like:
>
> onException(ZipFormatException.class).handled(true)
> .to("file:" + errorDir.toString()) //move corrupted zip to another directory
> .process(mailBuilder)
> .to("string-template:corruptedzipMail.tm")//doesn't work, attachment lost
> .to("smtp:"+ mailEndpoint);
>
> Processor mailBuilder = new Processor() {
>        @Override
>        public void process(Exchange exchange) throws Exception {
>                File file = ExchangeHelper.getMandatoryInBody(exchange, File.class);
>                exchange.getIn().setHeader("subject",
>                                Builder.constant("CAMEL TEST mail"));
>                exchange.getIn().addAttachment(file.getName(),
>                                new DataHandler(new FileDataSource(file)));
>        }
> };
>
>
> Claus Ibsen-2 wrote
>>
>> Hi
>>
>> What version of Camel are you using? And which template components are
>> you using?
>> Maybe post a snippet of your route.
>>
>>
>>
>> On Fri, Jan 6, 2012 at 11:32 AM, LNB &lt;lnijbijvank@&gt; wrote:
>>> In my camel route I download and unpack zip files. When something goes
>>> wrong,
>>> I want to send a mail with the zip file that has gone wrong as an
>>> attachment. I want to construct the body of this mail using a template.
>>>
>>> When I first add the zip file as an attachment to the message and next
>>> route
>>> it to a template, the attachment is lost. When I first route the message
>>> to
>>> the template, I lose the zipfile as message body and can't add it as
>>> attachment.
>>>
>>> Is it possible to use a template with an attachment?
>>>
>>> --
>>> View this message in context:
>>> http://camel.465427.n5.nabble.com/How-to-send-a-templated-mail-with-attachment-tp5125167p5125167.html
>>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>>
>>
>> --
>> Claus Ibsen
>> -----------------
>> FuseSource
>> Email: cibsen@
>> Web: http://fusesource.com
>> Twitter: davsclaus, fusenews
>> Blog: http://davsclaus.blogspot.com/
>> Author of Camel in Action: http://www.manning.com/ibsen/
>>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-send-a-templated-mail-with-attachment-tp5125167p5125742.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: How to send a templated mail with attachment?

Posted by LNB <ln...@kluwer.nl>.
I'm using camel 2.7.3 and a stringtemplate, but I could use an other template
if that would be useful.
My code looks like:

onException(ZipFormatException.class).handled(true)
.to("file:" + errorDir.toString()) //move corrupted zip to another directory
.process(mailBuilder)
.to("string-template:corruptedzipMail.tm")//doesn't work, attachment lost
.to("smtp:"+ mailEndpoint);

Processor mailBuilder = new Processor() {
	@Override
	public void process(Exchange exchange) throws Exception {
		File file = ExchangeHelper.getMandatoryInBody(exchange, File.class);
		exchange.getIn().setHeader("subject",
				Builder.constant("CAMEL TEST mail"));
		exchange.getIn().addAttachment(file.getName(),
				new DataHandler(new FileDataSource(file)));
	}
};


Claus Ibsen-2 wrote
> 
> Hi
> 
> What version of Camel are you using? And which template components are
> you using?
> Maybe post a snippet of your route.
> 
> 
> 
> On Fri, Jan 6, 2012 at 11:32 AM, LNB &lt;lnijbijvank@&gt; wrote:
>> In my camel route I download and unpack zip files. When something goes
>> wrong,
>> I want to send a mail with the zip file that has gone wrong as an
>> attachment. I want to construct the body of this mail using a template.
>>
>> When I first add the zip file as an attachment to the message and next
>> route
>> it to a template, the attachment is lost. When I first route the message
>> to
>> the template, I lose the zipfile as message body and can't add it as
>> attachment.
>>
>> Is it possible to use a template with an attachment?
>>
>> --
>> View this message in context:
>> http://camel.465427.n5.nabble.com/How-to-send-a-templated-mail-with-attachment-tp5125167p5125167.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
> 
> 
> 
> -- 
> Claus Ibsen
> -----------------
> FuseSource
> Email: cibsen@
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
> 


--
View this message in context: http://camel.465427.n5.nabble.com/How-to-send-a-templated-mail-with-attachment-tp5125167p5125742.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to send a templated mail with attachment?

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

What version of Camel are you using? And which template components are
you using?
Maybe post a snippet of your route.



On Fri, Jan 6, 2012 at 11:32 AM, LNB <ln...@kluwer.nl> wrote:
> In my camel route I download and unpack zip files. When something goes wrong,
> I want to send a mail with the zip file that has gone wrong as an
> attachment. I want to construct the body of this mail using a template.
>
> When I first add the zip file as an attachment to the message and next route
> it to a template, the attachment is lost. When I first route the message to
> the template, I lose the zipfile as message body and can't add it as
> attachment.
>
> Is it possible to use a template with an attachment?
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-send-a-templated-mail-with-attachment-tp5125167p5125167.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/