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 );
}
}