【问题标题】:How to send e-mail with attachments using Camel SMTP component and Camel XML DSL route definitions?如何使用 Camel SMTP 组件和 Camel XML DSL 路由定义发送带有附件的电子邮件?
【发布时间】:2014-07-04 14:57:01
【问题描述】:

我有以下路由定义文件:

<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 中有没有办法做到这一点?

【问题讨论】:

标签: java spring email apache-camel


【解决方案1】:

我找到了一种方法,但它需要额外的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 heliwell 向我指出了一个类似的问题。

【讨论】:

  • 我将在目录中创建文件后立即将文件作为附件发送到电子邮件。是否可以更改路线这样做?我试图像这样更改“来自”标签 并用 ${header.CamelFileName} 替换“test.zip”,但这不是一次成功的尝试。你能帮忙吗?
猜你喜欢
  • 2014-06-11
  • 2013-10-31
  • 1970-01-01
  • 2020-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
相关资源
最近更新 更多