Viktor
Viktor

Reputation: 1338

How to send e-mail with attachments using Camel SMTP component and Camel XML DSL route definitions?

I have the following route definitions file:

<routes xmlns="http://camel.apache.org/schema/spring">
    <route>
        <setHeader headerName="from">
            <constant>[email protected]</constant>
        </setHeader>
        <setHeader headerName="to">
            <constant>[email protected]</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://[email protected]?password=secret"/>
    </route>
</routes>

It is possible to send e-mail with attachments using Java DSL:

Endpoint endpoint = camelContext.getEndpoint(
        "smtp://[email protected]?password=secret");
Exchange exchange = endpoint.createExchange();
Message in = exchange.getIn();
Map<String, Object> headers = new HashMap<>();
headers.put("from", "[email protected]");
headers.put("to", "[email protected]");
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?

Upvotes: 3

Views: 8680

Answers (1)

Viktor
Viktor

Reputation: 1338

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.

Upvotes: 5

Related Questions