.emlファイルをMimeMessagesに変換し、添付ファイルとインライン画像をファイルに保存するコード:
// 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;
}
}
これはほとんどのemlファイルで機能しますが、次の例外が発生することがあります。
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"
そして、保存されたファイルは空です。eml-ファイルはMozillaThunderbirdによって生成されました。この例外の発生を防ぐにはどうすればよいですか?添付ファイルは間違いなくそこにあり、有効な映画/画像ファイルです。
編集:saveFileメソッドを使用します。
編集:ファイルには実際にいくつかの部分が欠けているようです。そのため、メールの送信またはダウンロード時に問題が発生しました。