2015-07-10 67 views
0

所以我創建了一個程序,而不使用javamail與gmail imaps連接。我設法登錄,但我正在努力尋找打開文件夾的方式。我已經看過每個和所有的答案都是javamail,我寧願不使用,因爲我自己學習這個,但是我似乎無法找到java api中的任何內容,這將允許我在不使用javamail的情況下打開此文件。試圖打開gmail文件夾沒有javamail

這裏是我的代碼:

public class CRole { 
    private BufferedReader socketSIn = null; 
    private PrintWriter socketSOut = null; 
    private Socket client; 
    public CRole(){ 
     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 { 
      socketSIn = new BufferedReader(new InputStreamReader(client.getInputStream())); 
      socketSOut = new PrintWriter(client.getOutputStream(), true); 
     } 
    catch(IOException e) { 
      System.out.println("Read failed"); 
      System.exit(+1); 
     } 
} 
    public void send_connectStringToS(String payload) { 
    this.socketSOut.println(payload); 
} 
    public String receive_acceptedStringFromS() { 
    String line = ""; 
    try { 
      line = this.socketSIn.readLine(); 
    } 
    catch(IOException e) { 
      System.out.println("Input/Outpur error."); 
      System.exit(+1); 
    } 
    return line; 
} 

還有更多的代碼,但我不認爲這是真正需要這個問題。基本上,我必須使用javamail還是有其他選擇嗎?

另外還有一種更快捷的縮進方式,因爲當我使用空格鍵時,它總是避開。

+1

如果你正在尋找一個API,你猜對了,Javamail是要走的路。否則,你可以嘗試創建自己的IMAP實現,祝你好運。 –

+1

如果你真的想從頭開始,實際的協議記錄在RFC3501中。 – Max

+0

是的,我一直在閱讀它一段時間,並不明白該怎麼做。但是,這比我想象的要容易得多。 – spaga

回答

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獲取1主體[文本]詢問您想要的任何消息。請注意,排名第一的人會獲取您帳戶中的第一封電子郵件,而不是最新的電子郵件。