有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我对Java非常陌生,对端口、服务器或socket一无所知,所以我希望有人能帮助我。我想做一个小程序,可以打印你收件箱中的所有邮件(已读和未读)

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

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) 个答案