java 如何通过camel spring DSL将文件作为邮件附件发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11035278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send file as mail attachment via camel spring DSL
提问by Czar
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 2.9.x 进行集成。其中一个路由包含两个端点 - 文件轮询端点和 smtp 邮件端点。第一个端点生成的文件必须通过 smtp 端点作为附件发送。
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.
对于 Camel 配置,我们使用 Spring DSL(这实际上是一个要求)。Spring 版本是 3.1.1。不幸的是,我只找到了在骆驼路由中将文件附加到电子邮件的 java dsl 示例和文档。
<endpoint uri="file:///path/to" id="file-source"/>
<endpoint uri="smtp://mail.example.com:25/[email protected]&password=secret&[email protected]" id="mail-dest"/>
<route id="simplified-for-readability">
<from ref="file-source"/>
<to ref="mail-dest"/>
</route>
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?
此配置将文件作为纯文本/文本正文发送,而不是作为附件(甚至是二进制文件)发送。有没有办法在不使用 Java dsl 的情况下将文件作为附件发送?
回答by Petter Nordlander
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.
这可以通过 Spring 配置来完成,但您可能必须编写一个简单的 java bean 左右,尽管这与 spring 或 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>
回答by Simon
This worked for me, a slight variation on what's above.
这对我有用,上面的内容略有不同。
import javax.activation.DataHandler;
import javax.mail.util.ByteArrayDataSource;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
public class AttachmentAttacher implements Processor {
private final String mimetype;
public AttachmentAttacher(String mimetype) {
this.mimetype = mimetype;
}
@Override
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(new ByteArrayDataSource(file, mimetype)));
}
}
回答by IVIike
For me, the following code snippet works without setting a MIME-type:
对我来说,以下代码片段无需设置 MIME 类型即可工作:
(Of course, you have it to adapt it for your needs)
(当然,您可以根据自己的需要进行调整)
public class MyProcesor implements org.apache.camel.Processor {
@Override
public void process(Exchange exchange) throws Exception {
String tempDir = System.getProperty("java.io.tmpdir");
File file = new File(tempDir + "/filename.ext" ); //or wherever you file is located
exchange.getIn().addAttachment("id", new DataHandler(new FileDataSource(file))); //no explicit MIME-type needed
}
}
But, I dont have this code for Spring DSL...
但是,我没有这个用于 Spring DSL 的代码......
回答by Spina
You might be able to do this with an expression such as simple. Simple is nice because it comes with Camel but I don't think it's powerful enough to do what you want. I haven't tried it but I'm sure that a groovyexpression could do this. A groovy expression can be specified in Spring.
您也许可以使用诸如simple 之类的表达式来执行此操作。简单很好,因为它与 Camel 一起提供,但我认为它的功能不足以做你想做的事。我还没有尝试过,但我确信groovy表达式可以做到这一点。可以在 Spring 中指定 groovy 表达式。