0

I want to merge multiple .eml files into one. By using simple merge technique I am able to merge it. But while trying to open it, i can only find one email inside it. Can anyone help me with it, as i am not sure what is wrong.

below is the code snip..

public static void mergeFiles(List<InputStream> source, File mergedFile) throws FileNotFoundException, IOException {
    FileWriter fstream = null;
    BufferedWriter out = null;
    try {
        fstream = new FileWriter(mergedFile, true);
        out = new BufferedWriter(fstream);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
  //  for (InputStream f : source) {

        Iterator itr = source.iterator();
        while(itr.hasNext())
        {
        //System.out.println("merging: " + f.getName());
        InputStream fis;
        fis =  (InputStream) itr.next();
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
        String aLine;
        while ((aLine = in.readLine()) != null) {
            out.write(aLine);
            out.newLine();
        }
        in.close();
    }
    out.close();
}
Anirudh
  • 1
  • 2
  • 1
    I am not sure of what you are really trying to achieve here. A .eml file is supposed to contain one single mail. Of course, there is a bunch of possible mailbox formats that you could use (traditional Unix, thuderbird, etc.), or alternatively you could remove all headers and only concat the body of all messages. It is certainly possible to build a MIME-multipart message containing several mails, but it is no longer a simple operation. – Serge Ballesta Feb 20 '18 at 14:12
  • We are having a requirement of merging multiple mail conversations happened to a single trailing mail and save it to local system. for that we are trying to download mails to .eml file and merge them. – Anirudh Feb 20 '18 at 14:30

0 Answers0