I have a javax.mail
application that parses through emails and gets the InputStream
for all application/*
attachments:
private DataInputStream getAttachmentStream(Message message) throws MessagingException, IOException {
if (message.isMimeType("multipart/*")) {
Multipart mp = (Multipart) message.getContent();
for (int p = 0; p < mp.getCount(); p++) {
BodyPart part = mp.getBodyPart(p);
if (part.getContentType().toLowerCase().startsWith("application")) {
InputStream is = part.getInputStream();
DataInputStream dis = new DataInputStream(is);
App.logger.info("Found attachment."");
return dis;
}
}
}
App.logger.warn("No attachment found.");
return null;
}
My problem is that even for emails that have an attachment, the resultant DataInputStream
is empty. I've stepped through in the debugger, and part
is definitely the correct Message
part with the attachment.
I switched the protocol of my code that checks the email address to use POP3 instead of IMAP, and this code magically worked. Can anyone explain why this code works for POP3 and not for IMAP?