有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有以下路由定义文件:

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

但我只需要使用XMLDSL来执行
有没有办法在骆驼上做到这一点


共 (1) 个答案

  1. # 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>
    

    这是我的工作

    附言:感谢马特·海利韦尔为我指出了一个类似的问题