Lo all,
I've written a simple program that connects to a POP3 server, allows me
to authenticate and then retrieves a certain message. The problem I'm
having is that when it retrives the message and prints it to the
console, I get different results depending on if I use a while loop or a
do-while loop. When I use a while loop, I get some of the headers but no
message body. When I use a do-while loop, I get some of the same
different headers (and message body), but some of the headers that I got
from using a while loop are missing. Does anyone have any idea what the
problem is?
My code as it is now:
import java.net.*;
import java.io.*;
public class mail
{
public static void main(String[] args) throws Exception
{
BufferedReader user = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Server address: ");
String host = user.readLine();
System.out.print("Port: ");
int port = Integer.parseInt(user.readLine());
// Connect to server and set up input/output streams
Socket mySocket = new Socket(host, port);
InputStream in = mySocket.getInputStream();
BufferedReader from = new BufferedReader(new
InputStreamReader(in));
OutputStream out = mySocket.getOutputStream();
PrintWriter to = new PrintWriter(new OutputStreamWriter(out));
String response = from.readLine();
System.out.println(response);
// Authenticate
System.out.print("Username: ");
String username = user.readLine();
to.println("USER " + username);
to.flush();
response = from.readLine();
System.out.println(response);
System.out.print("Password: ");
String pass = user.readLine();
to.println("PASS " + pass);
to.flush();
response = from.readLine();
System.out.println(response);
// Get message no. 5
to.println("RETR 5");
to.flush();
do
{
System.out.println(from.readLine());
}while(from.readLine() != null);
// Close streams and connection
to.println("QUIT");
to.close();
from.close();
mySocket.close();
if(mySocket.isClosed())
System.out.println("Disconnected");
}
}
Thanks.