I am having troubles with parsing MIME messages. I have some PCAP files with packets which contain specific attachments I want. I can retrieve full attachment from a mail, which has only a single attachment (content type: application/octet stream) not more. When I try to get multiple attachments I get only around 70% from each attachment. Is it problem with Java Mail parser or am I doing something wrong?
public ArrayList<Attachment> parseMessage() {
ArrayList<Attachment> attachments = new ArrayList<>();
try {
Session s = Session.getDefaultInstance(new Properties());
InputStream is = new ByteArrayInputStream(parts.getBytes());
MimeMessage message = new MimeMessage(s, is);
if(!message.getContentType().contains("multipart"))
return null;
Multipart multipart = (Multipart) message.getContent();
for (int j = 0; j < multipart.getCount(); j++) {
Part part = multipart.getBodyPart(j);
if (part.getDisposition() != null && part.getDisposition().equals("attachment") && part.getContentType().contains("application")) {
attachments.add(new Attachment(this.searchForContent(part),part.getFileName()));
}
}
// }
} catch (MessagingException ex) {
} catch (IOException ex) {
}
return attachments;
}
public byte[] searchForContent(Part part) {
InputStream is = null;
ArrayList<Byte> list = new ArrayList<>();
try {
is = part.getInputStream();
int character = 0;
while ((character = is.read()) != -1) {
list.add((byte) character);
}
} catch (Exception ex) {
}
byte[] bytes = new byte[list.size()];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = list.get(i);
}
return bytes;
}