读书人

javamail遍历的模式拿到邮件的正文和所

发布时间: 2012-07-26 12:01:08 作者: rapoo

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

读书人网 >网络基础

热点推荐