Ejemplo n.º 1
0
  public synchronized void open(
      String host, int port, String username, String password, boolean ssl)
      throws IOException, ProtocolException {
    if (username == null || password == null) {
      Authenticate auth = getAuthenticator();

      if (auth != null) {
        if (username == null) {
          User user = auth.getUser(host, "pop3", null);

          if (user != null) {
            username = user.getUsername();
            password = user.getPassword();
          }
        } else {
          password = auth.getPassword(host, "pop3", username);
        }
      }
    }

    Socket socket =
        ssl ? SSLSocketFactory.getDefault().createSocket(host, port) : new Socket(host, port);

    in = new ReadLineInputStream(socket.getInputStream());
    out = socket.getOutputStream();

    checkLine(readLine());
    sendCommand("user " + username);
    sendCommand("pass " + password);

    String s = sendCommand("stat");

    try {
      messages = new Message[getNumberOfMessages(s)];
    } catch (NumberFormatException e) {
      close();
      throw e;
    }

    for (int i = 0; i < messages.length; ++i) {
      messages[i] = new Message(i + 1);
    }
  }