I am using javamail (1.5.5)
to fetch mails from exchange 2010 server
. It fails to fetch few messages (java.lang.OutOfMemoryError: Java heap space
). But it is working fine when using java mail api 1.4.5
without heap space error.
What's strange here is the size of that peculiar message is 136516 Bytes. So when using IMAP, it should be fetched within 9 batches ( default fetch size is 16KB ). But from the debug trace referred below, something is strange.
Here is my code:
Message messObj = message[i]; String messSubject = null;
String messMsgId = null;
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
messObj.writeTo(bos);
bos.close();
javax.mail.util.SharedByteArrayInputStream bis = new javax.mail.util.SharedByteArrayInputStream(bos.toByteArray());
MimeMessage cmsg = new MimeMessage(session, bis);
bis.close(); mp.setMessage(cmsg);
messSubject = cmsg.getSubject();
messMsgId = ((MimeMessage) cmsg).getMessageID();
System.out.println("debugging:::subject::"+messSubject+"::msgid::"+messMsgId);
FLAGS (\Seen \Answered))| [14:48:21:205]|[09-26-2018]|[SYSOUT]|[INFO]|[73]: A3893 OK FETCH completed.| [14:48:21:205]|[09-26-2018]|[SYSOUT]|[INFO]|[73]: A3894 FETCH 1 (BODY[]<536512690.16384>)| [14:48:21:220]|[09-26-2018]|[SYSOUT]|[INFO]|[73]: * 1 FETCH (UID 91149 BODY[] {137921}|
FLAGS (\Seen \Answered))| [14:48:21:236]|[09-26-2018]|[SYSOUT]|[INFO]|[73]: A3894 OK FETCH completed.| [14:48:21:236]|[09-26-2018]|[SYSOUT]|[INFO]|[73]: A3895 FETCH 1 (BODY[]<536650611.16384>)| [14:48:21:251]|[09-26-2018]|[SYSOUT]|[INFO]|[73]: * 1 FETCH (UID 91149 BODY[] {137921}|
--5c3ef07b8737170331214bb435ce7be6--| [14:48:21:267]|[09-26-2018]|[SYSOUT]|[INFO]|[73]: FLAGS (\Seen \Answered))| [14:48:21:267]|[09-26-2018]|[SYSOUT]|[INFO]|[73]: A3895 OK FETCH completed.| [14:48:21:267]|[09-26-2018]|[SYSOUT]|[INFO]|[73]: A3896 FETCH 1 (BODY[]<536788532.16384>)| [14:48:21:283]|[09-26-2018]|[SYSOUT]|[INFO]|[73]: * 1 FETCH (UID 91149 BODY[] {137921}|
Error occurs before the system print. Each time the entire message is fetched and it is fetched for 3896 times ( after that memory heap error occurs and stops the process ). Could someone tell me what am I missing here?