I receive emails with imap. When I receive one of my emails, I need read the content of the email with javax.mail.Part.getContent() api, then I get the exception java.lang.ArrayIndexOutOfBoundsException: 16448. the email's mimeType is text/html.
I can get content in Other emails. Anybody could explain the reason, and how can I solve it.
This is my code.
public static void getMailTextContent(Part part, StringBuffer text, StringBuffer textPlain, StringBuffer textHtml) throws MessagingException, IOException {
boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;
if (part.isMimeType("text/*") && !isContainTextAttach) {
String content = "";
try{
content = (String) part.getContent();
}catch(UnsupportedEncodingException uex){
InputStream is = part.getInputStream();
content = convertIS2String(is);
} catch (ArrayIndexOutOfBoundsException aioobe) {
logger.info("ArrayIndexOutOfBoundsException happend.", aioobe);
throw aioobe;
} catch (IOException ioe) {
if (!ioe.getMessage().contains("No content")) {
throw ioe;
}
}
text.append(content);
if (part.isMimeType("text/plain")) {
textPlain.append(content);
}
if (part.isMimeType("text/html")) {
textHtml.append(content);
}
} else if (part.isMimeType("message/rfc822")) {
getMailTextContent((Part) part.getContent(), text, textPlain, textHtml);
} else if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
getMailTextContent(bodyPart, text, textPlain, textHtml);
}
}
}
This is exception stack trace.
java.lang.ArrayIndexOutOfBoundsException: 16448
at com.sun.mail.util.ASCIIUtility.toString(ASCIIUtility.java:248) ~[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.imap.protocol.FetchResponse.next20(FetchResponse.java:231) ~[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.imap.protocol.FetchResponse.parse(FetchResponse.java:219) ~[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.imap.protocol.FetchResponse.<init>(FetchResponse.java:96) ~[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:392) ~[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.iap.Protocol.command(Protocol.java:354) ~[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:2113) ~[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:2105) ~[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.imap.protocol.IMAPProtocol.fetchSectionBody(IMAPProtocol.java:1818) ~[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.imap.protocol.IMAPProtocol.fetchBody(IMAPProtocol.java:1801) ~[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.imap.protocol.IMAPProtocol.fetchBody(IMAPProtocol.java:1790) ~[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.imap.IMAPInputStream.fill(IMAPInputStream.java:156) ~[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.imap.IMAPInputStream.read(IMAPInputStream.java:192) ~[javax.mail-1.5.6.jar:1.5.6]
at java.io.FilterInputStream.read(FilterInputStream.java:83) ~[?:1.8.0_121]
at java.io.PushbackInputStream.read(PushbackInputStream.java:139) ~[?:1.8.0_121]
at com.sun.mail.util.QPDecoderStream.read(QPDecoderStream.java:86) ~[javax.mail-1.5.6.jar:1.5.6]
at com.sun.mail.util.QPDecoderStream.read(QPDecoderStream.java:165) ~[javax.mail-1.5.6.jar:1.5.6]
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) ~[?:1.8.0_121]
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) ~[?:1.8.0_121]
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) ~[?:1.8.0_121]
at java.io.InputStreamReader.read(InputStreamReader.java:184) ~[?:1.8.0_121]
at com.sun.mail.handlers.text_plain.getContent(text_plain.java:98) ~[javax.mail-1.5.6.jar:1.5.6]
at javax.activation.DataSourceDataContentHandler.getContent(DataHandler.java:795) ~[?:1.8.0_121]
at javax.activation.DataHandler.getContent(DataHandler.java:542) ~[?:1.8.0_121]
at javax.mail.internet.MimeBodyPart.getContent(MimeBodyPart.java:657) ~[javax.mail-1.5.6.jar:1.5.6]
at com.test.mail.web.api.component.receiver.imap.MailParser.getMailTextContent(MailParser.java:336) ~[classes/:?]
at com.test.mail.web.api.component.receiver.imap.MailParser.getMailTextContent(MailParser.java:369) ~[classes/:?]
at com.test.mail.web.api.component.receiver.imap.MailParser.parseMessage(MailParser.java:92) ~[classes/:?]
at
here is the debug info.
DEBUG IMAP: ignoring bad response, THROW:com.sun.mail.iap.ParsingException: error in FETCH parsing, unrecognized item at index 16416, starts with "ETCH Completed"
at com.sun.mail.imap.protocol.FetchResponse.parse(FetchResponse.java:218)
at com.sun.mail.imap.protocol.FetchResponse.<init>(FetchResponse.java:96)
at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:414)
at com.sun.mail.iap.Protocol.command(Protocol.java:395)
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:2158)
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:2150)
at com.sun.mail.imap.protocol.IMAPProtocol.fetchSectionBody(IMAPProtocol.java:1862)
at com.sun.mail.imap.protocol.IMAPProtocol.fetchBody(IMAPProtocol.java:1845)
at com.sun.mail.imap.protocol.IMAPProtocol.fetchBody(IMAPProtocol.java:1834)
at com.sun.mail.imap.IMAPInputStream.fill(IMAPInputStream.java:157)
at com.sun.mail.imap.IMAPInputStream.read(IMAPInputStream.java:198)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at java.io.PushbackInputStream.read(PushbackInputStream.java:139)
at com.sun.mail.util.QPDecoderStream.read(QPDecoderStream.java:87)
at com.sun.mail.util.QPDecoderStream.read(QPDecoderStream.java:167)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at com.sun.mail.handlers.text_plain.getContent(text_plain.java:101)
at javax.activation.DataSourceDataContentHandler.getContent(DataHandler.java:795)
at javax.activation.DataHandler.getContent(DataHandler.java:542)
at javax.mail.internet.MimeBodyPart.getContent(MimeBodyPart.java:683)
I config "mail.imap.partialfetch"=true works for me in Imap properties.
props.setProperty("mail.imap.partialfetch", "false");
I used POP3 instead of IMAP, then I can also get the mail body correctly.