How to send e-mail with attachments using Camel SMTP component and Camel XML DSL route definitions?
apache-camel, email, java, spring
Solution
I have found a way, but it requires additional `camel-script` component.
Replacing
<!-- <attachment id="attachment.zip" uri="resource:file:test.zip"/> -->
with
<filter>
<groovy>request.addAttachment("attachment.zip",
new javax.activation.DataHandler(
new javax.activation.FileDataSource("test.zip")))
</groovy>
<to uri="mock:dummy"/>
</filter>
does the job.
P.S.: Thanks to matt helliwell for pointing me to a similar question.
Problem
I have the following route definitions file: ``` <routes xmlns="http://camel.apache.org/schema/spring"> <route> <setHeader headerName="from"> <constant>user@mailserver.com</constant> </setHeader> <setHeader headerName="to"> <constant>john.smith@acme.com</constant> </setHeader> <setHeader headerName="subject"> <constant>Hello</constant> </setHeader> <setHeader headerName="contentType"> <constant>text/plain;charset=UTF-8</constant> </setHeader> <setBody> <constant>Test</constant> </setBody> <!-- <attachment id="attachment.zip" uri="resource:file:test.zip"/> --> <to uri="smtp://user@mailserver.com?password=secret"/> </route> </routes> ``` It is possible to send e-mail with attachments using Java DSL: ``` Endpoint endpoint = camelContext.getEndpoint( "smtp://user@mailserver.com?password=secret"); Exchange exchange = endpoint.createExchange(); Message in = exchange.getIn(); Map<String, Object> headers = new HashMap<>(); headers.put("from", "user@mailserver.com"); headers.put("to", "john.smith@acme.com"); headers.put("subject", "Hello"); headers.put("contentType", "text/plain;charset=UTF-8"); in.setHeaders(headers); in.setBody("Test"); in.addAttachment("attachment.zip", new DataHandler( applicationContext.getResource("file:test.zip").getURL())); Producer producer = endpoint.createProducer(); producer.start(); producer.process(exchange); ``` But I need to perform that using XML DSL only. Is there any way to do that in Camel?