next up previous contents
Next: Biff.java Up: Clients Previous: Evolution   Contents

Fetch.java

//Fetch reads a particular mail number off a POP(3) server.
//It does not delete the message.
// example command line:
//java Fetch mailnumber

// to use this code you must change these lines to the correct values
// and recompile the class:
//  String user = "yourname";
//  String pass = "yourpass";
//  String host = "yourhost";

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.Vector;

public class Fetch {
	
  public static int checkmail(int i) throws Exception 
  { Socket            talk=null;
    BufferedReader    inp=null;
    DataOutputStream  outs=null;
    StringTokenizer   st;
    StringBuffer      header;
    int               num_msgs=-1;
    String            toss, line;

    String user = "yourname";
    String pass = "yourpass";
    String host = "yourhost";

    try 
    { talk=new Socket(host,110);
      inp=new BufferedReader(new InputStreamReader(talk.getInputStream()));
      outs=new DataOutputStream(talk.getOutputStream());
      PrintWriter p = new PrintWriter(talk.getOutputStream());
      inp.readLine();
      p.println("user "+user); p.flush();
      inp.readLine();
      p.println("pass "+pass); p.flush();
      inp.readLine();
      p.println("top " + i + " 99999\n"); p.flush();
      while(true) 
      {	line=inp.readLine();
        if(line.equals(".") || line == null)
          break;
        System.out.println(line);
      }
      outs.writeBytes("QUIT\n");
      outs.flush();
      outs.close();
      outs=null;
    } 
    catch (Exception e) 
    { if(outs != null) 
      { outs.writeBytes("QUIT\n");
        outs.flush();
        outs.close();
        outs=null;
    }
    throw e;
  }
  finally 
  { if(outs != null) 
    { outs.writeBytes("QUIT\n");
      outs.flush();
      outs.close();
      outs=null;
     }
     if(talk != null)
       talk.close();	    
    }
     return num_msgs;	
  }

  public static void main(String args[]) 
  { try
    { checkmail(Integer.decode(args[0]).intValue());
    }
    catch(Exception e)
    { e.printStackTrace();
    }
  }
}


root 2004-03-16