IMAP:忽略错误响应,抛出:com.sun.mail.iap.ParsingException:FETCH解析错误,索引处无法识别的项目

时间:2018-11-01 05:55:46

标签: java email javamail

我想使用javax.mail.internet.MimeBodyPart.getContent()方法解析一封邮件的主体,我使用了Imap协议。但是,我面临一个大问题。我可以正确解析其他邮件。我的方法如下。

这是我获取邮件文本的方法。

// get mail text 
public static void getMailTextContent(Part part, StringBuffer text, StringBuffer textPlain, StringBuffer textHtml) throws Exception {
    boolean isContainTextAttach = part.getContentType().indexOf("name") > 0;
    if (part.isMimeType("text/*") && !isContainTextAttach) {
        String content = "";
        try{
            content = part.getContent().toString();
        }catch(UnsupportedEncodingException uex){
            logger.error("UnsupportedEncodingException,msg={}", uex.getMessage(), uex);
            InputStream is = part.getInputStream();
            content = convertIS2String(is);
        }catch (IOException ioe) {
            logger.error("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);
            try {
                getMailTextContent(bodyPart, text, textPlain, textHtml);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

这是邮件版本:

<dependency>
     <groupId>com.sun.mail</groupId>
     <artifactId>javax.mail</artifactId>
     <version>1.6.2</version>
</dependency>

这是调试信息:

    A37 OK FETCH Completed
DEBUG IMAP: ignoring bad response, THROW: 
com.sun.mail.iap.ParsingException: error in FETCH parsing, unrecognized item at index 16416, starts with "37 OK FETCH Complete..."
    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)

0 个答案:

没有答案