0

I am trying to connect gmail and get inbox from the server. I can connect to gmail and log in my account. When I send them to the socket

output.println( "A01 lOGIN " + userName + " " + password );
output.flush();

I can get response from the socket. But When I send new things:

output.println( "A02 SELECT INBOX");
JOptionPane.showMessageDialog( null,"Before Flush");
output.flush();
JOptionPane.showMessageDialog( null,"After Flush");

and try to get response from socket like that:

JOptionPane.showMessageDialog( null,in.nextLine());
JOptionPane.showMessageDialog( null,"After nextLine()");

It does not show any message dialog and wait almost 5 minutes. After 5 minutes, It shows empty message dialog (not showing the last message). I could not find any solution for that. Does anyone know what the problem is?

public static void main(String[] args) {

 // open SSLSocket connection to server and send login
    try { 

       // obtain SSLSocketFactory for creating SSLSockets
       SSLSocketFactory socketFactory = 
          ( SSLSocketFactory ) SSLSocketFactory.getDefault();

       // create SSLSocket from factory
       SSLSocket socket = 
          ( SSLSocket ) socketFactory.createSocket( 
             "imap.gmail.com", 993 ); 

       // create PrintWriter for sending login to server
       PrintWriter output = new PrintWriter( 
          new OutputStreamWriter( socket.getOutputStream() ) );

       Scanner in = null;
       in = new Scanner(socket.getInputStream());

       // display response to user
       JOptionPane.showMessageDialog( null, in.nextLine());

       // prompt user for user name
       String userName = JOptionPane.showInputDialog( null,
          "Enter User Name:" );

       // prompt user for password
       String password = JOptionPane.showInputDialog( null,
          "Enter Password:" );

       output.println( "A01 lOGIN " + userName + " " + password );
       output.flush();

       // display response to user
       JOptionPane.showMessageDialog( null, in.nextLine());
       JOptionPane.showMessageDialog( null, in.nextLine());

       output.println( "A02 SELECT INBOX");
       JOptionPane.showMessageDialog( null,"Before Flush");
       output.flush();
       JOptionPane.showMessageDialog( null,"After Flush");

       /*
        * It waits in nextLine() and it does not show anything
        * Because of waiting, It does not show After nextLine() message Dialog
        * But after 5 minutes, It shows a message dialog with null string
        */
       JOptionPane.showMessageDialog( null,in.nextLine());
       JOptionPane.showMessageDialog( null,"After nextLine()");

       // clean up streams and SSLSocket
       output.close();
       in.close();
       socket.close();

    } // end try

    // handle exception communicating with server
    catch ( IOException ioException ) { 
       ioException.printStackTrace(); 
    } 

    // exit application
    finally {
       System.exit( 0 );
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
HCD
  • 21
  • 1
  • 4
  • IMAP is a complex protocol, I would recommend using an existing library such as JavaMail rather than trying to code it by hand. – Ian Roberts Nov 01 '12 at 08:46
  • Sockets and buffered I/O don't work very well together. What is a Scanner? – Max Nov 01 '12 at 17:04
  • Ian Roberts, thank you for recommendation.Max, I used Scanner for reading server response. If I use BufferedReader rather than Scanner, It does not change anything.My problem is that If I try to run this code in a different computer, it works. But in my computer, it does not work, it waits a lot of time. I changed my security and privacy settings, but still does not change anything. – HCD Nov 01 '12 at 19:47
  • Did you try removing the BufferedReader and just use the InputStream by itself? If that is still blocking, then no data is arriving on the socket at all, which would indicate a networking issue. – Remy Lebeau Nov 02 '12 at 04:13
  • Yes, I tried InputStream. But it still does not work. In fact, I solved the problem. The problem is that I am using MAC OS X Version 10.8.2. When I run this code on Windows 7, It handles \n and \r. But in MAC OS X, it does not handle them. When I add \r in output, It works. – HCD Nov 02 '12 at 15:16

0 Answers0