你好﹐本來我想整理一下成最簡單的形式
但是我沒有空時間﹐所以只好貼一堆代碼放在這里
你可以自己看一下
把包的相關信息去掉就可以用了
然后打出對你有用的來
就可以了﹐OK//一個字串處理的方法
package org.fs.kit.str;import java.io.PrintStream;
import java.util.ArrayList;public class StrTools {
  public StrTools() {}  /**
   * Split a string by some special character
   * @param input the string you wanna split
   * @param div the special character
   * @return ArrayList of the string split results
   */
  public static ArrayList Split(String input, String div) {
    ArrayList rnList = null;
    ArrayList sepList = divSplit(div, "^");
    if(sepList.size() == 1) {
      rnList = divSplit(input, div);
    }
    else {
      String nxtDiv = "";
      rnList = new ArrayList();
      for(int i = 0; i < sepList.size() - 1; i++)
        nxtDiv = String.valueOf(nxtDiv) + String.valueOf(String.valueOf(i <= 0 ? "" : "^") + String.valueOf((String)sepList.get(i)));      ArrayList aryList = Split(input, (String)sepList.get(sepList.size() - 1));
      for(int i = 0; i < aryList.size(); i++)
        rnList.add(Split((String)aryList.get(i), nxtDiv));
    }
    return rnList;
  }  public static void main(String args[]) {
    String ttt = "als;hflskfweorihdsfkjhweoriuwoijf";
    ArrayList aryList = Split(ttt, "s");
  }
}

解决方案 »

  1.   

    //郵件相關類GetMail.java---------------------
    package org.fs.kit.mail;import java.util.*;
    import java.io.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;import org.fs.kit.str.StrTools;public class GetMail {
      public static String host     = "";
      public static String user     = "";
      public static String password = "";
      // the parameter for get mail
      static String protocol        = "pop3";
      static String mbox            = "inbox";
      static String url             = null;
      static int port               = 110;
      static boolean verbose        = true;
      static boolean showStructure  = false;  public GetMail() {}
      public GetMail(String mHost, String mUser, String mPassword) {
        this.host     = mHost;
        this.user     = mUser;
        this.password = mPassword;
      }  public static void dumpPart(Part p)
          throws Exception {
        // dispose the envelope if exist
        if (p instanceof Message) {
          dumpEnvelope((Message)p);
        }
        // output the type of content
        System.out.println("CONTENT-TYPE: " + p.getContentType());
        // output the file name
        String filename = p.getFileName();
        if (filename != null) {
          System.out.println("FILENAME: " + filename);
        }    if (p.isMimeType("text/plain")) { // dispose the text type
          System.out.println("text/plain>>>>");
          if (!showStructure)
            System.out.println((String)p.getContent());
        }
        else if (p.isMimeType("multipart/*")) { // dispose the multipart type
          System.out.println("multipart/*>>>>");
          Multipart mp = (Multipart)p.getContent();
          int count    = mp.getCount();
          for (int i = 0; i < count; i++)
            dumpPart(mp.getBodyPart(i));
        }
        else if (p.isMimeType("message/rfc822")) { // dispose message type
          System.out.println("message/rfc822>>>>");
          dumpPart((Part)p.getContent());
        }
        else if (!showStructure) { // detail message
          Object o = p.getContent();
          // print string
          if (o instanceof String) {
            System.out.println("String>>>>");
            System.out.println((String)o);
          }
          else if (o instanceof InputStream) {
            System.out.println("Stream>>>>");
            InputStream is = (InputStream)o;
            int c;
            while ((c = is.read()) != -1)
              System.out.write(c);
          }
          else {
            System.out.println("Unknown>>>>");
            System.out.println(o.toString());
          }
        }
        else {
          System.out.println("Unknown.");
        }
      }  public static void dumpEnvelope(Message m)
          throws Exception {
        System.out.println("Envelope----");    Address[] a;
        // sender
        if ((a = m.getFrom()) != null) {
          for (int j = 0; j < a.length; j++) {
            System.out.println("FROM: " + a[j].toString());
          }
        }
        // receiver
        if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
          for (int j = 0; j < a.length; j++) {
            System.out.println("TO: " + a[j].toString());
          }
        }
        // the title of mail
        System.out.println("SUBJECT: " + m.getSubject());
        // sent time
        Date d = m.getSentDate();
        System.out.println("SendDate: " + (d != null?d.toString():"UNKNOWN"));
        // the mail signer, include system signer and user signer
        Flags flags     = m.getFlags();
        StringBuffer sb = new StringBuffer();
        Flags.Flag[] sf = flags.getSystemFlags();    boolean first = true;
        for (int i = 0; i < sf.length; i++) {
          String s;
          Flags.Flag f = sf[i];
          if (f == Flags.Flag.ANSWERED) {
            s = "\\Answered";
          }
          else if (f == Flags.Flag.DELETED) {
            s = "\\Deleted";
          }
          else if (f == Flags.Flag.DRAFT) {
            s = "\\Draft";
          }
          else if (f == Flags.Flag.FLAGGED) {
            s = "\\Flagged";
          }
          else if (f == Flags.Flag.RECENT) {
            s = "\\Recent";
          }
          else if (f == Flags.Flag.SEEN) {
            s = "\\Seen";
          }
          else
            continue; // skip it      if (first)
            first = false;
          else
            sb.append(' ');
          sb.append(s);
        }
        String[] uf = flags.getUserFlags();
        for (int i = 0; i < uf.length; i++) {
          if (first) {
            first = false;
          }
          else {
            sb.append(' ');
            sb.append(uf[i]);
          }
        }
        System.out.println("FLAGS: " + sb.toString());    // get X-MAILER
        String[] hdrs = m.getHeader("X-Mailer");
        if (hdrs != null) {
          System.out.println("X-Mailer: " + hdrs[0]);
        }
        else {
          System.out.println("X-Mailer NOT available");
        }
      }  public static void startGet() {
        boolean total = true;
        Folder folder = null;
        try {
          // get mail
          Properties props = System.getProperties();
          Session session  = Session.getDefaultInstance(props,null);      Store store      = session.getStore(protocol);
          store.connect(host,port,user,password);      folder           = store.getFolder(mbox);
          if (folder == null) {
            System.out.println("Invalid folder");
            System.exit(1);
          }
          folder.open(Folder.READ_ONLY);
          // get the number of messages
          int totalMessages = folder.getMessageCount();
          if (totalMessages == 0) {
            System.out.println("Empty folder!");
            folder.close(false);
            store.close();
            System.exit(1);
          }
          if (verbose) {
            int newMessages = folder.getNewMessageCount();
            System.out.println("Total messages = " + totalMessages);
            System.out.println("New messages   = " + newMessages);
          }
          if (total) {  // get all messages
            Message[] msgs = folder.getMessages();
            for (int i = 0; i < msgs.length; i++) {
              System.out.println("MESSAGE #" + (i+1) + ":");
              dumpEnvelope(msgs[i]); // for mailer
              dumpPart(msgs[i]);     // for mail content
            }
          }
          folder.close(false);
          store.close();
        }
        catch (Exception ex) {
          System.out.println("Oops, got exceptioin! " + ex.getMessage());
          ex.printStackTrace();
          System.exit(1);
        }
        System.exit(0);
      }  /**
       * dump the attached file from mail, and now only surpport the file which named by letters
       * @param p the mail message
       * @param path the file path
       * @throws Exception when the file operation error
       */
      public static void dumpAttach(Part p)
          throws Exception {
        dumpAttach(p,"");
      }  /**
       * dump the attached file to path from mail, and now only surpport the file which named by letters
       * @param p the mail message
       * @param path the file path
       * @throws Exception when the file operation error
       */
      public static void dumpAttach(Part p,String path)
          throws Exception {    String contentType = p.getContentType();    if (contentType.indexOf("name") > 0) {
          // get the attached file name
          ArrayList ctList = StrTools.Split(contentType,"=");
          String fileName = StrTools.Replace(ctList.get(ctList.size()-1).toString(),"\"","");      FileOutputStream fn = new FileOutputStream(path + fileName);
          Object o = p.getContent();
          if(o instanceof InputStream) {
            InputStream is = (InputStream)o;
            int c;
            while ((c = is.read()) != -1)
              fn.write(c);
          }
        }
        if (p.isMimeType("multipart/*")) { // dispose the multipart type
          Multipart mp = (Multipart)p.getContent();
          int count    = mp.getCount();
          for (int i = 0; i < count; i++)
            dumpAttach(mp.getBodyPart(i),path);
        }
      }  public static void main(String[] args) {
        System.out.println(".........................");
        GetMail gm = new GetMail("172.24.5.7","base.xia","dy2s6");
        gm.startGet();
      }
    }