Search code examples
javamacosimapgmail-imap

Imap gmail does not response using java SSLSocket on MAC


When I executed below code on window, it runs well but Mac pc does not.

//declaration is removed;
    socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        socket = (SSLSocket)socketFactory.createSocket("imap.gmail.com", 993);
        in = new DataInputStream(socket.getInputStream());
        output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));

    BufferedReader sin = new BufferedReader(new InputStreamReader(in));
    String line;
    output.println("a001 LOGIN uName pWord");//assume that uName pWord is write
    output.flush();// commit query immediately
    while ((line = sin.readLine()) != null) {
        System.out.println(line);
    }

The message when open socket returned normaly

  • OK Gimap ready for requests from 42.118.56.137 c14mb33681806itd

but when I execute Login statement It hang connection on without any response and never time out. I have tried to use non secured but it did not make sense.

Noted: 2 step verification is turn off.
Insecured app is enable.


Solution

  • PrintWriter.println uses a platform specific end of line terminator, which differs between windows and mac. IMAP requires that \r\n (not '\n') be sent as end of lines. You should use some method where you control the end of lines, like:

    output.print("a001 LOGIN uName pWord\r\n")