View Javadoc

1   /*
2    * @(#) $Id: FirstPlainPartExtractor.java,v 1.1.2.1 2004/09/29 00:57:59 otsuka Exp $
3    * $Revision: 1.1.2.1 $
4    * Copyright (c) 2000 Shin Kinoshita All Rights Reserved.
5    */
6   package com.ozacc.mail.fetch.impl.sk_jp;
7   
8   import java.io.IOException;
9   import javax.mail.Part;
10  import javax.mail.MessagingException;
11  import javax.mail.internet.ContentType;
12  
13  /***
14   * 最初に見つけたtext/plainパートの本文を得るPartHandlerです。
15   * <P>
16   * </P>
17   * @version $Revision: 1.1.2.1 $ $Date: 2004/09/29 00:57:59 $
18   * @author Shin
19   */
20  public class FirstPlainPartExtractor implements PartHandler {
21  
22  	private String text = null;
23  
24  	public boolean processPart(Part part, ContentType context) throws MessagingException,
25  																IOException {
26  		String type = part.getContentType();
27  		// Bug fixed. Thx > ei
28  		// http://www.sk-jp.com/cgi-bin/treebbs.cgi?kako=1&all=1292&s=1292
29  		if (!part.isMimeType("text/plain") && type != null && !type.trim().equalsIgnoreCase("text")) {
30  			return true;
31  		}
32  		text = (String)MultipartUtility.getContent(part);
33  		return false;
34  	}
35  
36  	public String getText() {
37  		return text;
38  	}
39  
40  	public static void main(String[] args) throws Exception {
41  		javax.mail.internet.MimeMessage msg = new javax.mail.internet.MimeMessage(
42  				javax.mail.Session.getDefaultInstance(System.getProperties(), null), System.in);
43  		FirstPlainPartExtractor h = new FirstPlainPartExtractor();
44  		MultipartUtility.process(msg, h);
45  
46  		System.out.println("This is the first detected text/plain part.");
47  		System.out.println(h.getText());
48  	}
49  }