By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,542 Members | 1,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,542 IT Pros & Developers. It's quick & easy.

Java and POP3 IO (?) problem

P: n/a
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.
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Nick Chorley wrote:
do
{
System.out.println(from.readLine());
}while(from.readLine() != null);


The above code will only print every other line.

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.