How to send file as mail attachment via camel spring DSL

点点圈 提交于 2019-12-05 02:31:45

问题


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.

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>

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?


回答1:


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>



回答2:


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



回答3:


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 groovy expression could do this. A groovy expression can be specified in Spring.



来源:https://stackoverflow.com/questions/11035278/how-to-send-file-as-mail-attachment-via-camel-spring-dsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!