`
dengyin2000
  • 浏览: 1208448 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

javamail遍历的方式拿到邮件的正文和所有附件

 
阅读更多
                        try {
                            Object content = message.getContent();
                            if (content instanceof String) {
                                Log.v("Content", content.toString());
                            }else {
                                Multipart mp = (Multipart) content;
                                for (int i = 0; i < mp.getCount(); i++) {
                                    BodyPart bodyPart = mp.getBodyPart(i);
                                    iteratorAttachment(bodyPart);
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }


   private void iteratorAttachment(Part p) throws
            MessagingException, IOException {
        if (p.isMimeType("text/*")) {
            String s = (String)p.getContent();
            boolean textIsHtml = p.isMimeType("text/html");
            //正文
            return ;
        }
        String disposition = p.getDisposition();
        if ((disposition != null) &&
                ((disposition.equals(Part.ATTACHMENT) ||
                        (disposition.equals(Part.INLINE))))) {
            //附件
            // part.getFileName(), part.getInputStream()
            return ;
        }

        if (p.isMimeType("multipart/alternative")) {
            // prefer html text over plain text
            Multipart mp = (Multipart)p.getContent();

            for (int i = 0; i < mp.getCount(); i++) {
                Part bp = mp.getBodyPart(i);
                iteratorAttachment(bp);
            }
        } else if (p.isMimeType("multipart/*")) {
            Multipart mp = (Multipart)p.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
                iteratorAttachment(mp.getBodyPart(i));
            }
        }
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics