如何通过camel spring DSL将文件作为邮件附件发送 [英] How to send file as mail attachment via camel spring DSL

查看:26
本文介绍了如何通过camel spring DSL将文件作为邮件附件发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在当前项目中使用 Camel 2.9.x 进行集成.其中一条路由包含两个端点 - 文件轮询端点和 smtp 邮件端点.第一个端点生成的文件必须通过 smtp 端点作为附件发送.

I'm using Camel 2.9.x for integration purposes in our current project. One of the routes consists of two endpoints - file polling endpoint and smtp mail endpoint. Files produced by the first endpoint must be sent through smtp endpoint as attachments.

对于 Camel 配置,我们使用 Spring DSL(这实际上是一个要求).Spring 版本是 3.1.1.不幸的是,我只找到了在骆驼路由中将文件附加到电子邮件的 java dsl 示例和文档.

For Camel configuration we're using Spring DSL (this actually is a requirement). Spring version is 3.1.1. Unfortunately, I've found only java dsl examples and documentation of attaching a file to a e-mail message in camel routes.

<endpoint uri="file:///path/to" id="file-source"/>
<endpoint uri="smtp://mail.example.com:25/?username=someuser@example.com&amp;password=secret&amp;to=recv@example.com" id="mail-dest"/>
<route id="simplified-for-readability">
  <from ref="file-source"/>
  <to ref="mail-dest"/>
</route>

此配置将文件作为纯文本/文本正文发送,而不是作为附件(甚至是二进制文件)发送.有没有办法在不使用 Java dsl 的情况下将文件作为附件发送?

This config sends files as plain/text body, not as attachments (even binary files). Is there a way to send files as attachments without using Java dsl?

推荐答案

这可以通过 Spring 配置来完成,但您可能需要编写一个简单的 java bean 左右,尽管这与 spring 或 java DSL 无关.

This could be done with Spring config, but you might have to code a simple java bean or so, although that does not have to do with spring or java DSL.

首先创建一个类似于这个的类(你可能需要在这里修复一些东西):

First create a class similar to this one (you might need to fix stuff here):

// Note: Content Type - might need treatment!
public class AttachmentAttacher{
   public void process(Exchange exchange){
      Message in = exchange.getIn();
      byte[] file = in.getBody(byte[].class);
      String fileId = in.getHeader("CamelFileName",String.class);
      in.addAttachment(fileId, new DataHandler(file,"plain/text"));
    }
}

然后只需连接一个弹簧豆并在您的路线中使用它.应该可以解决问题.

Then just wire up a spring bean and use it in your route. Should do the trick.

<bean id="attacher" class="foo.bar.AttachmentAttacher"/>

<route>
  <from ref="file-source"/>
  <bean ref="attacher"/>
  <to ref="mail-dest"/>
</route>

这篇关于如何通过camel spring DSL将文件作为邮件附件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆