简体   繁体   中英

How to send file as mail attachment via camel spring DSL

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?

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

For me, the following code snippet works without setting a MIME-type:

(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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM