View Javadoc

    1   /*
    2    * @(#) $Id: PlainPartExtractor.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
    10  import javax.mail.MessagingException;
    11  import javax.mail.Part;
    12  import javax.mail.internet.ContentType;
    13  import javax.mail.internet.MimeMessage;
    14
    15  /***
    16   * text/plainを結合した文字列を得るPartHandlerです。
    17   *
    18   * @version $Revision: 1.1.2.1 $ $Date: 2004/09/29 00:57:59 $
    19   * @author Shin
    20   */
    21  public class PlainPartExtractor implements PartHandler {
    22
    23  	private String text = null;
    24
    25  	public boolean processPart(Part part, ContentType context) throws MessagingException,
    26  																IOException {
    27  		if (!part.isMimeType("text/plain")) {
    28  			return true;
    29  		}
    30  		if (text == null) {
    31  			// 最初のテキストパートを無条件に抽出
    32  			text = (String)MultipartUtility.getContent(part);
    33  		} else {
    34  			String disposition = part.getDisposition();
    35  			if (disposition == null || disposition.equalsIgnoreCase(Part.INLINE)) {
    36  				text += "\r\n\r\n-- inline --\r\n\r\n" + (String)MultipartUtility.getContent(part);
    37  			}
    38  		}
    39  		return true;
    40  	}
    41
    42  	public String getText() {
    43  		return text;
    44  	}
    45
    46  	public static void main(String[] args) throws Exception {
    47  		MimeMessage msg = new MimeMessage(javax.mail.Session.getDefaultInstance(System
    48  				.getProperties(), null), System.in);
    49  		PlainPartExtractor h = new PlainPartExtractor();
    50  		MultipartUtility.process(msg, h);
    51
    52  		System.out.println("This is the detected text/plain parts.");
    53  		System.out.println(h.getText());
    54  	}
    55  }