gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:37 24 4
gpt4 key购买 nike

我有以下路由定义文件:

<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 中有什么方法可以做到这一点吗?

最佳答案

我找到了一种方法,但它需要额外的 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>

完成任务。

P.S.:感谢 matt helliwell 指出我类似的问题。

关于java - 如何使用 Camel SMTP 组件和 Camel XML DSL 路由定义发送带附件的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24557088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com