Can't read imap multipart messages.
843830Jul 6 2006 — edited Jul 7 2006Hi
I want to read and save messages to harddisk.
Messages with only TEXT/PLAIN are working.
But multipart messages with attachment or TEXT/PLAIN and TEXT/HTML
causes same result java.io.IOException: No content.
Is this a mail server problem or how to read and save multipart message content with imap.
It's working fine if I use pop3 protocol.
I have try 2 different mailserver and they are not working.
Is there somekind of trick how to read and save message parts?
messages.writeTo(System.out);
Print hole messages headers and content.
Part of the code and session.setDebug(true); trace.
It is modified code to demonstrate problem.
------------------------------------------
...
Store store = session.getStore("imap");
store.connect(host, username, password);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();
for (int i=0, n=message.length; i<n; i++) {
System.out.println(i+" ####################################################");
Object content = message[i].getContent();
if (content instanceof Multipart) {
mpart(message[i]);
} else {
System.out.println("Not Multipart message.................("+message[i].getContentType()+")");
}
}
...
public static void mpart(Message message){
for(int i=0;i<2;i++){
try{
Multipart mp = (Multipart)message.getContent();
Part part = mp.getBodyPart(i);
System.out.println("getFileName = "+part.getFileName());
System.out.println("getSize = "+part.getSize());
System.out.println("getLineCount = "+part.getLineCount());
System.out.println("getDisposition = "+part.getDisposition());
System.out.println("getContentType = "+part.getContentType());
System.out.println("getContent = "+part.getContent());
InputStream in = part.getInputStream();
byte[] buf = new byte[1024];
int n = in.read(buf);
}catch(Exception ex){System.out.println(ex.toString());}
}
...
----------------------------------------------------------------
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
* OK IMAP4 Ready XXXXXX 00020fc4
A0 CAPABILITY
* CAPABILITY IMAP4REV1 ACL QUOTA MAILBOX-REFERRALS NAMESPACE UIDPLUS SORT THREAD=ORDEREDSUBJECT THREAD=REFERENCES IDLE
A0 OK CAPABILITY
DEBUG: protocolConnect login, host=XXXXXXX, user=XXXXXXXX, password=<non-null>
A1 LOGIN XXXXXX XXXXXXX
A1 OK You are so in
DEBUG: connection available -- size: 1
A2 EXAMINE INBOX
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen)
* OK [PERMANENTFLAGS ()]
* 3 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1148984915]
* OK [UIDNEXT 44]
A2 OK [READ-ONLY] Completed
A3 LIST "" INBOX
* LIST (\Noinferiors) "/" "INBOX"
A3 OK Completed (0.000 secs 2 calls)
0 ####################################################
A4 FETCH 1 (BODYSTRUCTURE)
* 1 FETCH (BODYSTRUCTURE ("TEXT" "PLAIN" ("CHARSET" "iso-8859-1") NIL NIL "8BIT" 34 9 NIL NIL NIL))
A4 OK Completed (0.000 sec)
A5 FETCH 1 (BODY[TEXT]<0.34>)
* 1 FETCH (BODY[TEXT] {34}
ssss
s
s
ssssssss
ss
)
A5 OK Completed (0.000 sec)
Not Multipart message.................(TEXT/PLAIN; charset=iso-8859-1)
1 ####################################################
A6 FETCH 2 (BODYSTRUCTURE)
* 2 FETCH (BODYSTRUCTURE (("TEXT" "PLAIN" ("CHARSET" "iso-8859-1") NIL NIL "8BIT" 27 1 NIL NIL NIL)("IMAGE" "BMP" ("NAME" "FeatherTexture.bmp") NIL NIL "BASE64" 22894 NIL ("ATTACHMENT" ("FILENAME" "FeatherTexture.bmp")) NIL) "MIXED" ("BOUNDARY" "----=_20060703105059_53113") NIL NIL))
A6 OK Completed (0.000 sec)
getFileName = null
getSize = 27
getLineCount = 1
getDisposition = null
getContentType = TEXT/PLAIN; charset=iso-8859-1
A7 FETCH 2 (BODY[1]<0.27>)
* 2 FETCH ()
A7 OK Completed (0.000 sec)
java.io.IOException: No content
getFileName = FeatherTexture.bmp
getSize = 22894
getLineCount = -1
getDisposition = ATTACHMENT
getContentType = IMAGE/BMP; name=FeatherTexture.bmp
getContent = com.sun.mail.util.BASE64DecoderStream@b2a2d8
A8 FETCH 2 (BODY[2]<0.16384>)
* 2 FETCH ()
A8 OK Completed (0.000 sec)
java.io.IOException: No content
2 ####################################################
A9 FETCH 3 (BODYSTRUCTURE)
* 3 FETCH (BODYSTRUCTURE (("TEXT" "PLAIN" ("CHARSET" "iso-8859-1") NIL NIL "QUOTED-PRINTABLE" 249 13 NIL NIL NIL)("TEXT" "HTML" ("CHARSET" "iso-8859-1") NIL NIL "QUOTED-PRINTABLE" 4527 157 NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "----=_NextPart_000_0001_01C6861E.61D815D0") NIL NIL))
A9 OK Completed (0.000 sec)
getFileName = null
getSize = 249
getLineCount = 13
getDisposition = null
getContentType = TEXT/PLAIN; charset=iso-8859-1
A10 FETCH 3 (BODY[1]<0.249>)
* 3 FETCH ()
A10 OK Completed (0.000 sec)
java.io.IOException: No content
getFileName = null
getSize = 4527
getLineCount = 157
getDisposition = null
getContentType = TEXT/HTML; charset=iso-8859-1
A11 FETCH 3 (BODY[2]<0.4527>)
* 3 FETCH ()
A11 OK Completed (0.000 sec)
java.io.IOException: No content
A12 CLOSE
A12 OK Completed
DEBUG: added an Authenticated connection -- size: 1
A13 LOGOUT
* BYE LOGOUT received
A13 OK Completed
DEBUG: IMAPStore connection dead
DEBUG: IMAPStore cleanup, force false
DEBUG: IMAPStore cleanup done