home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / Mail For Me / Mail4ME.jar / de / trantor / mail / Pop3Client.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-10-21  |  5.0 KB  |  149 lines

  1. package de.trantor.mail;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6.  
  7. public class Pop3Client implements InboxClient {
  8.    private Connection socket;
  9.    private InputStream input;
  10.    private OutputStream output;
  11.    private boolean debug = false;
  12.  
  13.    public void open(String host, String user, String pass) throws Exception, IOException, Pop3Exception {
  14.       if (this.connected()) {
  15.          this.close();
  16.       }
  17.  
  18.       this.socket = Connection.createConnection();
  19.       this.socket.open(host, 110);
  20.       this.input = this.socket.getInputStream();
  21.       this.output = this.socket.getOutputStream();
  22.  
  23.       try {
  24.          this.execute((String)null);
  25.          this.execute("USER " + user);
  26.          this.execute("PASS " + pass);
  27.       } catch (Pop3Exception var5) {
  28.          this.socket.close();
  29.          this.input = null;
  30.          this.output = null;
  31.          this.socket = null;
  32.          throw var5;
  33.       }
  34.    }
  35.  
  36.    public void close() throws IOException, Pop3Exception {
  37.       if (this.connected()) {
  38.          this.execute("QUIT");
  39.       }
  40.  
  41.       this.socket.close();
  42.       this.input = null;
  43.       this.output = null;
  44.       this.socket = null;
  45.    }
  46.  
  47.    public boolean connected() {
  48.       return this.socket != null;
  49.    }
  50.  
  51.    private void send(String s) throws IOException, Pop3Exception {
  52.       if (this.debug) {
  53.          System.out.println("[POP3/SEND] " + s);
  54.       }
  55.  
  56.       this.output.write(s.getBytes());
  57.       this.output.write(13);
  58.       this.output.write(10);
  59.    }
  60.  
  61.    private String receive() throws IOException, Pop3Exception {
  62.       StringBuffer buffer = new StringBuffer("");
  63.  
  64.       char c;
  65.       do {
  66.          c = (char)this.input.read();
  67.          if (c != -1 && c != '\r' && c != '\n') {
  68.             buffer.append(c);
  69.          }
  70.       } while(c != '\r');
  71.  
  72.       if (this.debug) {
  73.          System.out.println("[POP3/RECV] " + buffer);
  74.       }
  75.  
  76.       return new String(buffer);
  77.    }
  78.  
  79.    private String execute(String command) throws IOException, Pop3Exception {
  80.       if (command != null) {
  81.          this.send(command);
  82.       }
  83.  
  84.       String result = this.receive();
  85.       if (result.length() > 1 && result.charAt(0) == '-') {
  86.          throw new Pop3Exception(result);
  87.       } else {
  88.          return result;
  89.       }
  90.    }
  91.  
  92.    public int getMessageCount() throws IOException, Pop3Exception {
  93.       String buffer = this.execute("STAT");
  94.       int space1 = buffer.indexOf(32);
  95.       int space2 = buffer.indexOf(32, space1 + 1);
  96.       return Integer.parseInt(buffer.substring(space1 + 1, space2));
  97.    }
  98.  
  99.    private Message receiveMessage() throws IOException, Pop3Exception {
  100.       Message message = new Message();
  101.       int count = 0;
  102.  
  103.       for(String buffer = this.receive(); !buffer.equals(""); buffer = this.receive()) {
  104.          if (buffer.length() > 1 && buffer.charAt(0) == '.') {
  105.             buffer = buffer.substring(1);
  106.          }
  107.  
  108.          if (!buffer.startsWith(" ") && !buffer.startsWith("\t")) {
  109.             message.addHeaderLine(buffer);
  110.             ++count;
  111.          } else {
  112.             message.setHeaderLine(count - 1, message.getHeaderLine(count - 1) + "\r\n" + buffer);
  113.          }
  114.       }
  115.  
  116.       for(String var4 = this.receive(); !var4.equals("."); var4 = this.receive()) {
  117.          if (var4.length() > 1 && var4.charAt(0) == '.') {
  118.             var4 = var4.substring(1);
  119.          }
  120.  
  121.          message.addBodyLine(var4);
  122.       }
  123.  
  124.       return message;
  125.    }
  126.  
  127.    public Message getMessage(int index) throws IOException, Pop3Exception {
  128.       this.execute("RETR " + (index + 1));
  129.       return this.receiveMessage();
  130.    }
  131.  
  132.    public Message getHeaders(int index) throws IOException, Pop3Exception {
  133.       this.execute("TOP " + (index + 1) + " 0");
  134.       return this.receiveMessage();
  135.    }
  136.  
  137.    public void removeMessage(int index) throws IOException, Pop3Exception {
  138.       this.execute("DELE " + (index + 1));
  139.    }
  140.  
  141.    public void setDebug(boolean debug) {
  142.       this.debug = debug;
  143.    }
  144.  
  145.    public boolean getDebug() {
  146.       return this.debug;
  147.    }
  148. }
  149.