I have a requirement to send files as email attachments.

a simple route for this would be 

from("file:///path/to/file.xyz")
to("smtp://mail-details...")

The behaviour for this is to use the message body (i.e. the file) as the
email contents, so that's not quite right.  I want the file to be an email
attachment and the email contents to be generated using a velocity template.
I've written a bean handler to do this (it's quite simple) so that I can do
the following

from("file:///path/to/file.xyz")
// copy the body to be a mime/encoded attachment on the message
.bean(new AttachmentAttacher("text/csv"))     
// overwrite the message body with the desired email content
.to("velocity:///path/to/email-contents.vm")
to("smtp://mail-details...")

See below for AttachmentAttacher code.

Would it be useful is AttachmentAttacher could be part of the camel-mail
component?
I can find some time next week to create a PR with unit test etc.
Is there a better way to make this part of the camel-mail component, so that
routes could be written

from("file:///path/to/file.xyz")
.bodyToAttachment("text/csv")
.to("velocity:///path/to/email-contents.vm")
to("smtp://mail-details...")

or is that a stupid idea / not possible to add to the route grammar 

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




--
View this message in context: 
http://camel.465427.n5.nabble.com/Easily-Send-Files-as-email-attachments-tp5765664.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to