javamail 解析eml文件的内容部分

彭星 发布于 2011/09/19 17:29
阅读 4K+
收藏 0

解析内容部分代码:

public void getMailContent(Part part,String key) throws Exception {

String contenttype=part.getContentType(); 
   System.out.println("contenttype   "+contenttype); 
    int nameindex=contenttype.indexOf("name"); 
    boolean conname=false; 
    if(nameindex!=-1)conname=true; 
    if(part.isMimeType("text/plain")&&!conname){ 
     bodytext.append((String)part.getContent());   
    }else if(part.isMimeType("text/html")&&!conname){ 
     bodytext.append((String)part.getContent());   
    } 
    else if(part.isMimeType("multipart/*")){ 
     Multipart multipart=(Multipart)part.getContent(); 
     int counts=multipart.getCount(); 
     for(int i=0;i<counts;i++){ 
      getMailContent(multipart.getBodyPart(i),""); 
     } 
    }else if(part.isMimeType("message/rfc822")){ 
     getMailContent((Part)part.getContent(),""); 
    } 
    else{} 
    System.out.println(bodytext);
}

运行后报错:

java.io.IOException: Error in encoded stream, got 1
 at com.sun.mail.util.BASE64DecoderStream.decode(BASE64DecoderStream.java:148)
 at com.sun.mail.util.BASE64DecoderStream.read(BASE64DecoderStream.java:53)
 at com.sun.mail.util.BASE64DecoderStream.read(BASE64DecoderStream.java:78)
 at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
 at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source)
 at sun.nio.cs.StreamDecoder.read(Unknown Source)
 at java.io.InputStreamReader.read(Unknown Source)
 at com.sun.mail.handlers.text_plain.getContent(text_plain.java:81)
 at javax.activation.DataSourceDataContentHandler.getContent(DataHandler.java:745)
 at javax.activation.DataHandler.getContent(DataHandler.java:501)
 at javax.mail.internet.MimeBodyPart.getContent(MimeBodyPart.java:564)
 at starsino.com.module.mail.reademl.ParseEmlDAO.getMailContent(ParseEmlDAO.java:186)
 at starsino.com.module.mail.reademl.ParseEmlDAO.getMailContent(ParseEmlDAO.java:192)
 at starsino.com.module.mail.reademl.ParseEmlDAO.readEmlFile(ParseEmlDAO.java:64)
 at starsino.com.bean.TestEml.main(TestEml.java:263)

各位大侠 求解释…… 最好有完整的解析 eml 文件源代码  UQQ 270778796@qq.com 小弟感激不尽

加载中
0
打一把
打一把

用这段代码再试试?

	/**
	 * 解析邮件,把得到的邮件内容保存到一个StringBuffer对象中,解析邮件
	 * 主要是根据MimeType类型的不同执行不同的操作,一步一步的解析
	 * @param part
	 * @return
	 * @throws Exception
	 */
	public String getMailContent(Part part) throws Exception {
		String contenttype = part.getContentType();
		int nameindex = contenttype.indexOf("name");
		StringBuffer bodytext = new StringBuffer();
		boolean conname = false;
		if (nameindex != -1)
			conname = true;
		if (part.isMimeType("text/plain") && !conname) {
			bodytext.append((String) part.getContent());
		} else if (part.isMimeType("text/html") && !conname) {
			bodytext.append((String) part.getContent());
		} else if (part.isMimeType("multipart/*")) {
			Multipart multipart = (Multipart) part.getContent();
			int counts = multipart.getCount();
			for (int i = 0; i < counts; i++) {
				return getMailContent(multipart.getBodyPart(i));
			}
		} else if (part.isMimeType("message/rfc822")) {
			return getMailContent((Part) part.getContent());
		} else {

		}

		return bodytext.toString();
	}



返回顶部
顶部