Warm tip: This article is reproduced from stackoverflow.com, please click
java apache-camel

Apache Camel: save email to file (.eml) using routes

发布于 2020-03-27 10:31:26

I am polling an email account, and that works great. I would like also to have a backup copy of the received messages as files (.eml). I have tried something like this:

from( mailurl+"?username="+username+"&password="+password+"&"+options)
.to( "file://backup?allowNullBody=false&forceWrites=true");

What I get is a file for every received email with a generated name like ID-MACHINENAME-2443-1211718892437-1-0 in the desired directory, but the file is empty. I know I can make my own .eml file using a processor, but, isn't there a more straightforward way to do it using just routes? Why is the created file empty?

Questioner
Josi
Viewed
65
Josi 2019-07-03 23:47

The solution I found for making a backup copy of every incoming email to .eml file is creating a processor like this:

    public void process(Exchange ex) throws Exception {

    javax.mail.Message mailMessage = ex.getIn(org.apache.camel.component.mail.MailMessage.class).getMessage();

    File file = new File( "backupDirectory", "DesiredFileName.eml");
    DataOutputStream os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));       
    mailMessage.writeTo(os);
    os.close();
}

This solution copies the whole message, including all headers, body and attachments.