3

我使用以下代码来读取消息对象的正文:

Object content = _message.getContent();
String body = null;
if (content instanceof String) {
    body = (String) content;
} else if (content instanceof Multipart) {
    Multipart multipart = (Multipart) content;
    BodyPart part = multipart.getBodyPart(0);
    body = (String) part.getContent();
}

当内容是多部分时,一切正常,但是当内容只是文本/纯文本时,我得到以下异常(已经在第 1 行的 getContent() 调用中!):

13.01.2011 17:22:23 org.zkoss.zk.ui.impl.UiEngineImpl handleError:1253
SCHWERWIEGEND: >>org.zkoss.zk.ui.UiException: java.io.IOException
java.io.IOException
at  javax.mail.internet.MimePartDataSource.getInputStream(MimePartDataSource.java:108)
at com.sun.mail.handlers.text_plain.getContent(text_plain.java:90)
at javax.activation.DataSourceDataContentHandler.getContent(DataHandler.java:775)
at javax.activation.DataHandler.getContent(DataHandler.java:522)
at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1396)

我还尝试了 JavaMail FAQ 中显示的代码:http ://www.oracle.com/technetwork/java/faq-135477.html#mainbody

结果相同。

该消息是通过在 IMAPFolder 实例上调用 getMessages(n) 来检索的。文件夹实例来自 IMAPStore 对象。

我完全不知道可能出了什么问题......有人有什么想法吗?

4

2 回答 2

5

妈的,我发现了问题。我需要在调用 getContent() 之前打开该文件夹。以下代码现在运行良好:

Folder folder = _message.getFolder();
// Open folder in read-only mode
if (folder.isOpen()) {
    if ((folder.getMode() & Folder.READ_WRITE) != 0) {
        folder.close(false);
        folder.open(Folder.READ_ONLY);
    }
} else {
    folder.open(Folder.READ_ONLY);
}

Object content = _message.getContent();
String body = null;
if (content instanceof String) {
    body = (String) content;
} else if (content instanceof Multipart) {
    Multipart multipart = (Multipart) content;
    BodyPart part = multipart.getBodyPart(0);
    body = (String) part.getContent();
}
if (folder.isOpen()) {
    folder.close(false);
}

我仍然只是想知道为什么这个问题只影响纯/文本电子邮件并且在我尝试获取多部分消息时没有发生。

于 2011-01-13T16:47:52.930 回答
0

我以以下方式编写了我的代码

public void setBody(Message msg) {

    try {
        if (msg.isMimeType("text/plain") || msg.isMimeType("text/html")) {
            try {
                // body += (String) msg.getContent() + " ";

                if (msg.getContent() instanceof String) {
                    body += (String) msg.getContent() + " ";
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block

            }
        }
        else if (msg.getContent() instanceof Multipart) {

            Multipart multipart = null;
            try {
                multipart = (Multipart) msg.getContent();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            boolean flag = false;
            for (int i = 0; i < multipart.getCount(); i++) {
                BodyPart bodyPart = multipart.getBodyPart(i);

                String disposition = bodyPart.getDisposition();

                if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
                    if (!flag) {
                        flag = true;
                    }

                    javax.activation.DataHandler handler = bodyPart.getDataHandler();
                    String filename = handler.getName();
                    body += filename + " ";
                } else {
                    String bodyText = null;
                    try {
                        bodyText = GetMessageBodyText(bodyPart);
                    } catch (IOException e) {

                        e.printStackTrace();
                    }
                    body += bodyText + " ";
                }
            }
        }
    } catch (MessagingException e) {

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
于 2012-07-15T21:09:06.180 回答