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

你可能感兴趣的:(javamail遍历的方式拿到邮件的正文和所有附件)