I am encountering a strange problem when reading message with multipart/alternative parts (through IMAP4), and attachments. I suspect it is a bug and if so, it would appear to be a very serious one.
It appears that the headers for the alternative parts are read correctly, however, when reading the headers for the following attachment parts, I seem to get the wrong headers.
Upon further investigation, it would appear that when I use getAllHeaders() on the part immediately following the alternative part (first attachment), I get the header for the first alternative (i.e. text/plain, ...), when I read the headers for the next attachment, I get the headers for the second alternative (html), and on the next one, I suspect I am getting the headers for the actial multipart/alternative.
On the fourth attachment I get the headers for the first attachment, on the fifth I get the one which should match the second, and so forth...
To make things even more strange, when I use getFileName() on any of the parts I get the correct file name - unlike what I get when I used getHeader(...).
Here is a sample printout from a program which parses the bodyparts and prints the headers (notice the mismatches in
bold):
$$$$$$$$$$$$ ALTERNATIVE $$$$$$$$$$$$$$$$
========== (com.sun.mail.imap.IMAPBodyPart@13ad085) =============
getContentType= multipart/alternative; boundary="-=_qa142b7d804_b"
getFileName= null
========== END =============
========== (com.sun.mail.imap.IMAPBodyPart@4fce71) =============
getContentType= text/plain; charset=iso-8859-1
getFileName= null
header: name=Content-Type, value=text/plain; charset="iso-8859-1"
header: name=Content-Transfer-Encoding, value=7bit
========== END =============
========== (com.sun.mail.imap.IMAPBodyPart@1faba46) =============
getContentType= text/html; charset=US-ASCII
getFileName= null
header: name=Content-Type, value=text/html
header: name=Content-Transfer-Encoding, value=7bit
========== END =============
$$$$$$$$$$$$$$$ ATTACHMENTS $$$$$$$$$$$$$$$$$$
========== (com.sun.mail.imap.IMAPBodyPart@12b3d53) =============
getContentType=
application/msword; name=test_doc
getFileName= test_doc
header: name=Content-Type,
value=text/plain; charset="iso-8859-1"
header: name=Content-Transfer-Encoding, value=7bit
========== END =============
========== (com.sun.mail.imap.IMAPBodyPart@1c74f37) =============
getContentType=
image/gif; name=test_gif
getFileName= test_gif
header: name=Content-Type,
value=text/html
header: name=Content-Transfer-Encoding, value=7bit
========== END =============
========== (com.sun.mail.imap.IMAPBodyPart@21b220) =============
getContentType= text/html; name=test_html
getFileName= test_html
========== END =============
========== (com.sun.mail.imap.IMAPBodyPart@3570b0) =============
getContentType= text/html;
name=test_htm
getFileName= test_htm
header: name=Content-Type, value=application/msword;
name="test_doc word.doc"
header: name=Content-Transfer-Encoding, value=base64
header: name=Content-Disposition, value=attachment;
filename="test_doc word.doc"
========== END =============
========== (com.sun.mail.imap.IMAPBodyPart@79717e) =============
getContentType= image/pjpeg;
name=test_jpeg
getFileName= test_jpeg
header: name=Content-Type, value=image/gif;
name="test_gif gif.gif"
header: name=Content-Transfer-Encoding, value=base64
header: name=Content-Disposition, value=attachment;
filename="test_gif gif.gif"
========== END =============
The source code which produced this output:
private void printDebug(Part message) throws IOException, MessagingException {
System.out.println("========== (" + message.toString() + ") =============");
System.out.println("getContentType= " + message.getContentType());
System.out.println("getFileName= " + message.getFileName());
Enumeration enum = message.getAllHeaders();
if (enum != null) {
while (enum.hasMoreElements()) {
Header header = (Header)enum.nextElement();
System.out.println("header: name=" + header.getName() +
", value=" + header.getValue());
}
}
if (message.isMimeType("multipart/*")) {
Multipart mp = (Multipart)message.getContent();
int count = mp.getCount();
System.out.println("Count = " + count);
for (int i=0; i<count; i++) {
BodyPart bp = mp.getBodyPart(i);
printDebug(bp);
}
}
}
I am inclined to report a bug, but I find it strange that no one encountered a similar situation in the past. Any help would be greatly appreciated.
Amit Tal