سؤال

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.

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top