Java - POP3 - 下載附件
下載附件參考
https://www.codejava.net/java-ee/javamail/download-attachments-in-e-mail-messages-using-javamail
先判斷是否有附件,看ContentType是否為multipart
String contentType = message.getContentType();
if(contentType.contains("multipart")){
//JUST DO IT
}
然後取得內容並轉成Mutipart,並輸入為檔案
Multipart multiPart = (Multipart)message.getContent();
for (int j = 0; j < multiPart.getCount(); j++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(j);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
// code to save attachment...
String destFilePath = downloadDir + "/"+ part.getFileName();
FileOutputStream output = new FileOutputStream(destFilePath);
InputStream input = part.getInputStream();
byte[] buffer = new byte[4096];
int byteRead;
while ((byteRead = input.read(buffer)) != -1) {
output.write(buffer, 0, byteRead);
}
output.close();
}
}
然後就,完成了。