Im trying to download all new e-mails attachments from my email with javamail and through imap. It all works fine, how ever some attachments are in pdfs and decoded as BASE64, which my statement
if (msg.getContent() instanceof Multipart)
dont catch, since its com.sun.mail.util.BASE64DecoderStream@33c7e1bb.
How do I get this attachment and download it to my hard drive?
Tried this so far, and the first IF statement catches the attachments with Base64 decoding.
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("");
}
}
Can also use NodeJS if there is a simple and easy way to download attachments to my disk. Tried node-imap but I got the same error there.