0

Code that converts .eml files to MimeMessages and saves the attachments and inline images to files:

           // fileList contains paths to eml files    
           for (File file : fileList) {
                MimeMessage mail = Utility.mailFromFile(file);
                if (mail == null) {
                    os.println("Error: " + file.getAbsolutePath()
                            + " has an unsupported format.");
                    continue;
                }
                try {
                    MimeBodyPart bPart = (MimeBodyPart) content.getBodyPart(i);
                    for (int i = 0; i < content.getCount(); i++) {
                        BodyPart bPart = content.getBodyPart(i);
                                        // sort out messages but include inline images
                        if (bPart.getFileName() == null) {
                            continue;
                        }
                                        String savePath = outputDirectory.getAbsolutePath() + "\\" + bPart.getFileName();
                        File f = new File(savePath);
                                            // generate new file name in case file already exists
                        f = Utility.getSaveFile(f);
                        bPart.saveFile(f);
                    }
                } catch (Exception ex) {
                    os.println("Error: " + ex.getMessage());
                    continue;
                }
            }

This works for most eml files but sometimes I get the following exception:

Error: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 2 before EOF, the 10 most recent characters were: "GJMIX5FF\r\n"

And the saved file is empty. The eml-Files were generated by Mozilla Thunderbird. How do I prevent this exception from happening? The attachments are definitely there and valid movie/image files.

Edit: Now using the saveFile method.

Edit: Seems like the files are really missing some parts. So there was a problem when sending or downloading the mails.

Kazuo
  • 387
  • 5
  • 12
  • user below link may be usefull for u http://stackoverflow.com/questions/13358134/send-image-in-email-body-using-java/14832757#14832757 – Lucky Feb 12 '13 at 13:11

1 Answers1

1

I would need to see the entire message to see if there really is a base64 encoding error.

What version of JavaMail are you using? There have been a few bugs in this area in older versions.

There is one serious error in your code, that may or may not be related to the error you're seeing. As described in the javadocs for the Part.getSize method, it may not return the exact size of the part. You should read the data from the InputStream until EOF. Or better yet, use the MimeBodyPart.saveFile method.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • Thanks for the answer. I did read from the stream without relying on the part size before, but the same problem occured. I'm now using the MimeBodyPart.saveFile method as you suggested. Still the same error occurs and the exported files are corrupt. The interesting thing is, that Thunderbird displays the images just fine while the exported photo for example is missing Pixels at the bottom. Online Base64 decoders show the error message "Invalid length for a Base-64 char array.". Example image: [link](http://pastebin.com/YMS9Q1DV) – Kazuo Feb 10 '13 at 12:24
  • Seems like the files are really missing some parts. So there was a problem when sending or downloading the mails. Thunderbird just cut off the image above the missing pixels so the image looked complete. Thanks for the answer. – Kazuo Feb 10 '13 at 12:52