如何使用Camel SMTP组件和Camel XML DSL路由定义发送带附件的电子邮件?

3

我有一个路由定义文件,内容如下:

<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>

使用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);

但我需要仅使用XML DSL执行它。
是否有一种在Camel中实现的方式?


可能是重复问题 https://dev59.com/XmTWa4cB1Zd3GeqPIfri - matt helliwell
1个回答

5

我找到了一种方法,但需要额外的camel-script组件。

替换

<!-- <attachment id="attachment.zip" uri="resource:file:test.zip"/> -->

使用

<filter>
    <groovy>request.addAttachment("attachment.zip",
            new javax.activation.DataHandler(
            new javax.activation.FileDataSource("test.zip")))
    </groovy>
    <to uri="mock:dummy"/>
</filter>

工作是否正常。

附言:感谢Matt Helliwell指向类似的问题。


我将在文件被创建后,作为附件将其发送到电子邮件中。是否可以更改路由以实现此操作?我尝试更改“from”标签,如<from uri="file://camel/input"/>,并将“test.zip”替换为${header.CamelFileName},但尝试失败了。请问您能提供帮助吗? - Gollie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接