How to send file as mail attachment via camel spring DSL

走远了吗. 提交于 2019-12-03 20:50:30

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>

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

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.

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