尝试在没有javamail的情况下打开gmail文件夹

时间:2015-07-10 08:16:50

标签: java imap directory

所以我创建了一个程序而不使用javamail连接gmail imaps。我已设法登录但我正在努力寻找打开文件夹的方法。我已经看了所有的答案都是javamail,我不想使用,因为我自己也学习这个,但是我似乎无法在java api中找到任何可以让我在不使用javamail的情况下打开这个文件

这是我的代码:

{{1}}

代码更多,但我认为这个问题确实不需要。基本上,我必须使用javamail还是有替代方案?

还有一种更快的缩进方式,因为当我使用空格键时,它总是会出现问题。

1 个答案:

答案 0 :(得分:0)

 public class Imaps  {
     private static BufferedReader in = null;
     private static PrintWriter out = null;
     private static Socket client;
     public static void main (String []arg){
    SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    try {
        client = (SSLSocket) sslsocketfactory.createSocket("imap.gmail.com", 993);
        System.out.println("Connect to Host");

    }
    catch(IOException e) {
        System.out.println("Unable to listen on ports");
        System.exit(+1);
    }
    try {
        in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        out = new PrintWriter(client.getOutputStream(), true);
        out.println("abcd CAPABILITY");
        String response =  (String) in.readLine();
        System.out.println("Server: " + response);

        out.println("efgh STARTTLS"); //tls part does not work but will not cause any problems being there.
        response =  (String) in.readLine();
        System.out.println("Server: " + response);

        String payload3 = "a001 login username @gmail.com password";
        out.println(payload3);

        response =  (String) in.readLine();
        System.out.println("Server: " + response);
        response =  (String) in.readLine();
        System.out.println("Server: " + response);
        response =  (String) in.readLine();
        System.out.println("Server: " + response);
        //should say success if you enter correct password.


        String payload5 = "a002 select inbox";
        //String payload5 = "a002 select inbox";
        out.println(payload5);

        response =   (String) in.readLine();
        System.out.println("Server: " + response);
        response =  (String) in.readLine();
        System.out.println("Server: " + response);
        response =  (String) in.readLine();
        System.out.println("Server: " + response);
        response =   (String) in.readLine();
        System.out.println("Server: " + response);


    }
    catch(IOException e) {
        System.out.println("Read failed");
        System.exit(+1);
    }

答案是,当您发送a002选择收件箱时,无需将文件夹存储在打开的任何位置。在此之后,您可以使用a004 fetch 1 body [text]来询问您想要的任何消息。请注意,排名第一的是您帐户中的第一封电子邮件,而不是最新的电子邮件。