javamail无法读取multipart / mixed邮件

时间:2014-04-09 12:47:35

标签: java email javamail

我无法检索“multipart / MIXED”邮件正文的内容......

这是我用来阅读邮件的内容....

private String read(Message message) throws MessagingException, IOException {
        String result = message.getContentType().toString() + " Unable to read";
        if (message instanceof MimeMessage) {
            MimeMessage m = (MimeMessage) message;
            Object contentObject = m.getContent();
            if (contentObject instanceof Multipart) {
                BodyPart clearTextPart = null;
                BodyPart htmlTextPart = null;
                Multipart content = (Multipart) contentObject;
                int count = content.getCount();
                for (int i = 0; i < count; i++) {
                    BodyPart part = content.getBodyPart(i);
                    if (part.isMimeType("text/plain")) {
                        clearTextPart = part;
                        break;
                    } else if (part.isMimeType("text/html")) {
                        htmlTextPart = part;
                    }
                }
                if (clearTextPart != null) {
                    result = (String) "<html><body>"
                            + clearTextPart.getContent() + "</body></html>";
                } else if (htmlTextPart != null) {
                    String html = (String) htmlTextPart.getContent();
                    result = android.text.Html.fromHtml(html).toString();
                }
            } else if (contentObject instanceof String) {
                String html = (String) contentObject;
                result = html;
            } else {
                result = "not found";
            }
        }
        return result;
    }

正如您所看到的那样,邮件甚至无法通过(消息实例为MimeMessage) ......

“multipart / MIXED”邮件的输出是:

multipart/MIXED; boundary=20cf306..... Unable to read 

2 个答案:

答案 0 :(得分:2)

public static String processBody( > Part p < , String operacao)    
 } else if (p.isMimeType("multipart/*")) {
                *** MimeMessage m = (MimeMessage) p; ***
                Multipart mp = (Multipart) m.getContent();
                for (int i = 0; i < mp.getCount(); i++) {
                    t = processBody(mp.getBodyPart(i), operacao);
                    if (t != null)
                        return t;
                }
            }

答案 1 :(得分:1)

您还没有提供足够的细节。当然,您可以通过该代码跟踪程序的流程。究竟发生了什么?如果它没有通过其中一项测试,那么您看到的实际值是什么以及您期望的是什么?

您使用的是什么版本的JavaMail?

您是否正在从邮件服务器阅读邮件? debug output显示什么?