Hello all :) I'm having troubles parsing mails from a pop3 server. In the end I want to get the attachements. The mails are multiparts, but so far I only managed to get a small part of the mail.
Here is the original email, as read by outlook (+one file attached):
ID-G1619161
P.S. : This is an automated email.
Write back to [email protected]
Post-scriptum :
This message is confidential. [...]
Here is some code:
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages();
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
System.out.println("message : " + message);
System.out.println("***");
IOUtils.copy(message.getInputStream(),System.out);
System.out.println("***");
System.out.println("message.getContentType : " + message.getContentType());
Multipart multiPart = (Multipart) message.getContent();
System.out.println("multiPart : " + multiPart);
int numberOfParts = multiPart.getCount(); // line A
}
This prints:
message : org.apache.geronimo.javamail.store.pop3.message.POP3Message@79f6f296
***
P.S. : This is an automated email.***
message.getContentType : multipart/mixed; boundary="qMm9M+Fa2AknHoGS"
multiPart : javax.mail.internet.MimeMultipart@bc92535
And throws on line A (multiPart.getCount
!):
javax.mail.MessagingException: Unexpected response: Write back to [email protected]
My take on this is the message is formatted in a weird way that is not recognised by javax.mail
, because getCount
is trying to read the rest of the mail.
I can even get the whole message! (Trying to do this at: IOUtils.copy(message.getInputStream(),System.out);
)
How would you debug this? Also, if you know a way to get the whole mail as a string (attachements included), I'd be very glad you share it. I'm prepared to parse this by hand.
Best regards
JavaMail debugging tips are here.
You can get the entire message text using the Message.writeTo method. Write it to a FileOutputStream or a ByteArrayOutputStream.
You might have better luck using the JavaMail reference implementation instead of the Geronimo(?) version you're using.