开发者

Why does Javamail throw an IOException if a plain/text message should be opened?

开发者 https://www.devze.com 2023-02-04 23:58 出处:网络
I use the following code to read the body of a message object: Object content = _message.getContent();

I use the following code to read the body of a message object:

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();
}

When the content is multipart, everything works fine, but when the content is just text/plain, I get the following exception (at the getContent() call in line 1 already!):

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.m开发者_开发百科ail.internet.MimeMessage.getContent(MimeMessage.java:1396)

I also tried the code shown in the JavaMail FAQ: http://www.oracle.com/technetwork/java/faq-135477.html#mainbody

Same result.

The message was retrieved by using calling getMessages(n) on an IMAPFolder instance. The folder instance comes from a IMAPStore object.

I am completely out of ideas what could be going wrong... Does anyone have some ideas?


Damn, I found the issue. I need to open the folder BEFORE calling getContent(). The following code works nicely now:

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);
}

I'm still just wondering why the issue affected only plain/text emails and didn't occur when I tried to fetch Multipart messages.


I wrote my code in following manner

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();
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消