IMAP with Java Socket, PrinterWriter, and BufferedReader
I'm trying to write an IMAP email client in Java without using the Java Mail API.
Basically my problem is that I only receive * OK IMAP4 ready and
* BYE IMAP server terminating connection in my console. For some reason the server isn't receiving my other commands or I'm not getting the responses. Anyone have any ideas? I've commented out some lines where I was trying to figure out how to receive multiple lines from the server, without knowing how many lines there would be.
Code:
Socket s;
try {
s = new Socket("imap.aol.com", 143);
// create an input stream and tie it to the socket
InputStream in;
in = s.getInputStream();
BufferedReader sin = new BufferedReader(new InputStreamReader(in));
PrintWriter output = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
//String thisLine = sin.readLine();
/*do
{ // while loop begins here
System.out.println(thisLine);
} while ((thisLine = sin.readLine()) != null);
output.flush();*/
System.out.println(sin.readLine());
output.println(". login myemail@aol.com mypass");
System.out.println(sin.readLine());
System.out.println(sin.readLine());
/*while ((thisLine = sin.readLine()) != null)
{ // while loop begins here
System.out.println(thisLine);
} // end while
output.flush();*/
output.println(". list \"\" \"*\"");
System.out.println(sin.readLine());
System.out.println(sin.readLine());
System.out.println(sin.readLine());
output.println(". logout");
System.out.println(sin.readLine());
}
Thanks!