There are a couple issues below. One is that Resharper is telling me that the serverResponse value not be used in any execution path. The other has to do with getting the headers for each message. If I issue a TOP 1 0, it does not throw any errors, it just hangs there. If I issue a TOP 1 0 followed by an Environment.NewLine, it tells me "-ERR no such message." I got the code from the following link.
for (int i = 0; i <= messageCount; i++)
{
_data = "TOP " + i + " 0";
_sizeOfData = Encoding.ASCII.GetBytes(_data.ToCharArray());
_networkStream.Write(_sizeOfData,0,_sizeOfData.Length);
serverResponse = _streamReader.ReadLine();
while (true)
{
serverResponse = _streamReader.ReadLine();
if (serverResponse == ".") break;
if (serverResponse.Length > 4)
{
if (serverResponse.Substring(0, 5) == "From:")
lstMessages.Items.Add(serverResponse);
}
}
}
Why are you sending raw bytes instead of the text as was originally in the article? In fact, I can’t actually get your code to compile because
GetBytesreturns abyte[]butWriteexpects achar[]. I can change it to this and at least it compiles:But all that does is mask a potential problem that you’ll probably not run into anyway, namely non-ASCII characters. (Your code will cause question marks to appear for non-ASCII characters which if they ever occurred would cause you a headache to debug, but they should never occur in this case.) Instead you can just do this:
Anyway, one of the reasons your code hangs is because the code in the article is using
WriteLine()which sends aNewLinefor you. So either useWriteLine()(recommended) or change your string to:Another reason for hanging is that you’re not flushing after each line:
One more reason you might be hanging is if you’ve got a ton of messages and you’re processing all of this on the UI thread. Don’t mix complexities, output to the
Consoleor something else that you can monitor in realtime until you’ve figured out the network part, then add your object/collection.As for the error message, after plugging your snippet of code into the master code from that article and fixing what I mentioned above I am unable to reproduce the error. But if you have no messages available for POP3 download then
TOP 1 0would produce that message.