i’m trying to access my gmail inbox with java by using socket (not java mail api).
i succesfully establish the connection but select inbox command is not recognized. here is my code. and the output i get.
SSLSocketFactory sslsocketfactory = HttpsURLConnection.getDefaultSSLSocketFactory();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("imap.gmail.com", 993);
BufferedReader sin = new BufferedReader(new InputStreamReader(sslsocket.getInputStream()));
PrintWriter output = new PrintWriter(new OutputStreamWriter(sslsocket.getOutputStream()));
String line;
output.println("A004 LOGIN my-mail-here my-pass-here");
output.println("A005 SELECT [GMAIL]/myinbox");
output.println("A006 LOGOUT");
output.flush();
while ((line = sin.readLine()) != null)
System.out.println(line);
and here is the output i get :
* OK Gimap ready for requests from 78.185.154.27 o19if1214052bkq.44
A005 BAD Unknown command o19if1214052bkq.44
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE
A004 OK my-mail@gmail.com my-name my-surname authenticated (Success)
what may be the problem? login works but select doesn’t work. i tried starttls , it also doesn’t work.
Because you’re doing it wrong 🙂will do what you are trying to do.
A great way to test out these sorts of things is with openssl
here’s an example using my gmail account. My Drafts is listed as:
That being said, what I think is happening is that the imap server is async, and you’re not waiting for it to authenticate you before sending the select command – that’s why the error is coming back before the login result in your output