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.