【问题标题】:how to handle multipart/alternative in android app using javamail?如何使用 javamail 在 android 应用程序中处理 multipart/alternative?
【发布时间】:2013-06-24 08:42:36
【问题描述】:

我无法解析多部分电子邮件内容。有人可以帮忙吗?

我正在使用 stackoverflow 上的帖子中的提示来使用 javamail 接收和解析电子邮件(来自 gmail)。 Adding activation.jar & mail.jar to Android App

message getContent API 返回 com.sun.mail.imap.IMAPMessage@423ff878

这意味着它不是由 javamail 数据源处理的。

内容如下:

内容类型:多部分/替代;边界=mimepart_51c2a32167465_82e9b701343f

--mimepart_51c2a32167465_82e9b701343f 内容类型:文本/纯文本;字符集=utf-8 内容传输编码:引用打印 内容处置:内联

...文本...

--mimepart_51c2a32167465_82e9b701343f 内容类型:文本/html;字符集=utf-8 内容传输编码:引用打印 内容处置:内联

http://www.w3.org/1999/xhtml">

--mimepart_51c2a32167465_82e9b701343f--

【问题讨论】:

    标签: android email multipart


    【解决方案1】:

    JavaMail API FAQ 中给出的以下代码可能会有所帮助:

    private boolean textIsHtml = false;
    
        /**
         * Return the primary text content of the message.
         */
        private String getText(Part p) throws
                    MessagingException, IOException {
            if (p.isMimeType("text/*")) {
                String s = (String)p.getContent();
                textIsHtml = p.isMimeType("text/html");
                return s;
            }
    
            if (p.isMimeType("multipart/alternative")) {
                // prefer html text over plain text
                Multipart mp = (Multipart)p.getContent();
                String text = null;
                for (int i = 0; i < mp.getCount(); i++) {
                    Part bp = mp.getBodyPart(i);
                    if (bp.isMimeType("text/plain")) {
                        if (text == null)
                            text = getText(bp);
                        continue;
                    } else if (bp.isMimeType("text/html")) {
                        String s = getText(bp);
                        if (s != null)
                            return s;
                    } else {
                        return getText(bp);
                    }
                }
                return text;
            } else if (p.isMimeType("multipart/*")) {
                Multipart mp = (Multipart)p.getContent();
                for (int i = 0; i < mp.getCount(); i++) {
                    String s = getText(mp.getBodyPart(i));
                    if (s != null)
                        return s;
                }
            }
    
            return null;
        }
    

    (附注:我不确定这个答案现在对你有用,但如果有人遇到同样的问题,我会发布以供将来参考)。

    【讨论】:

      猜你喜欢
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-15
      相关资源
      最近更新 更多