I am using following Java
code to try extract email attachments:
private static List<File> extractAttachment(Message message) {
List<File> attachments = new ArrayList<File>();
try {
Multipart multipart = (Multipart) message.getContent();
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
System.out.println("bodyPart.getDisposition(): " + bodyPart.getDisposition());
if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
continue; // dealing with attachments only
}
InputStream is = bodyPart.getInputStream();
String filePath = "/tmp/" + bodyPart.getFileName();
System.out.println("Saving: " + filePath);
File f = new File(filePath);
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buf)) != -1) {
fos.write(buf, 0, bytesRead);
}
fos.close();
attachments.add(f);
}
} catch (IOException | MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return attachments;
}
However, I always get bodyPart.getDisposition(): null
. Any clue how should I extract inline attachments?
Thanks
P.S.: I am using Apple Mail
client on my Mac for sending test emails with attachment. Email client however should not be of concern.