我試圖從我的電子郵件與javamail和通過imap下載所有新的電子郵件附件。這一切工作正常,怎麼過一些附件是PDF和base64解碼,這我的發言Javamail下載base64附件
if (msg.getContent() instanceof Multipart)
不抓,因爲它的[email protected]。
如何獲取此附件並將其下載到我的硬盤?
迄今嘗試過,第一條IF語句通過Base64解碼捕獲附件。
for (Message msg : messages) {
Address[] fromAddress = msg.getFrom();
String from = fromAddress[0].toString();
String subject = msg.getSubject();
String sentDate = msg.getSentDate().toString();
String messageContent = "";
String attachFiles = "";
System.out.println(msg.getContent());
if (msg.getContent() instanceof BASE64DecoderStream)
{
BASE64DecoderStream base64DecoderStream = (BASE64DecoderStream) msg.getContent();
byte[] byteArray = IOUtils.toByteArray(base64DecoderStream);
}
if (msg.getContent() instanceof Multipart) {
Multipart multipart = (Multipart) msg.getContent();
for (int i = 0; i < multipart.getCount(); i++) {
Part part = multipart.getBodyPart(i);
String disposition = part.getDisposition();
if ((disposition != null) &&
((disposition.equalsIgnoreCase(Part.ATTACHMENT) ||
(disposition.equalsIgnoreCase(Part.INLINE))))) {
MimeBodyPart mimeBodyPart = (MimeBodyPart) part;
String fileName = mimeBodyPart.getFileName();
attachFiles += fileName;
File fileToSave = new File(fileName);
mimeBodyPart.saveFile(saveDirectory + File.separator + fileToSave);
System.out.println("saved attachment: " + fileName + " to disk at: " + saveDirectory);
}
}
}
System.out.println("\t From: " + from);
System.out.println("\t Subject: " + subject);
System.out.println("\t Sent Date: " + sentDate);
System.out.println("\t Attachments: " + attachFiles);
System.out.println("");
}
}
如果有簡單方便的方法將附件下載到我的磁盤上,也可以使用NodeJS。嘗試node-imap,但我得到了同樣的錯誤。
[將Java InputStream的內容寫入OutputStream的簡單方法]的可能重複(https://stackoverflow.com/questions/43157/easy-way-to-write-contents-of-a-java-inputstream -to-AN-的OutputStream) – jmehrens