如何打印所有收件箱电子邮件

时间:2014-06-23 20:15:41

标签: java sockets ssl imap ports

我对Java很陌生,对端口或服务器或套接字一无所知,所以我希望有人可以帮助我。我想制作一个小程序,打印收件箱中的所有邮件(已阅读和未阅读)。

我在网上找到了这个代码,但它只在我发送的文件夹中给出了我最早的消息。

public class EmailService {
  String server = "pop.gmail.com";
  int port = 995;
  static String username;
  static String password;

  SSLSocket socket;
  BufferedReader input;
  PrintWriter output;

  public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      username = s.next() + "@gmail.com";
      password = s.next();
      new EmailService();
  }

  public EmailService() {
    try {
        SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        socket = (SSLSocket)sslsocketfactory.createSocket(server, port);
        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        // After each println you MUST flush the buffer, or it won't work properly.
        // The true argument makes an automatic flush after each println.
        output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
        connect();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
  }

  public void connect() throws IOException {
    System.out.print("Greeting message: ");
    String response = readOneLine();
    System.out.println(response);

    // Username
    output.println("USER " + username);
    response = readOneLine();
    System.out.println(response);

    // Password
    output.println("PASS " + password);
    response = readOneLine();
    System.out.println(response);

    output.println("RETR 1");
    while (!response.equals(".")) {
        response = readOneLine();
        System.out.println(response);
    }
  }

  public String readOneLine() throws IOException {
    return input.readLine();
  }
}    

如何更改此代码以获取收件箱中的所有邮件?

0 个答案:

没有答案