The first is in a Windows Application
private void Pop3Check()
{
string serverResponse;
int messageCount;
string emailFrom = "";
string emailSubject = "";
try
{
// Create a TCP client for a TCP connection
client = new TcpClient();
client.Connect("pop.gmail.com", 995);
}
catch (SocketException ex)
{
MessageBox.Show(ex.Message);
}
// Create a network stream to retrieve data from the TCP client
NetworkStream stream = client.GetStream(); //<-- stops executing here
// We need a stream reader to be able to read the network stream
StreamReader reader = new StreamReader(stream);
StreamWriter writer = new StreamWriter(stream);
try
{
serverResponse = reader.ReadLine(); //Get opening POP3 banner
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
writer.WriteLine("USER " + txtUser.Text); //Send username
writer.Flush();
serverResponse = reader.ReadLine();
if (serverResponse.Substring(0, 4) == "-ERR")
{
status += "Unable to log into server";
return;
}
writer.WriteLine("PASS " + txtPass.Text); //Send password
writer.Flush();
try
{
serverResponse = reader.ReadLine();
}
catch (IOException)
{
status += "Unable to log into server";
return;
}
if (serverResponse.Substring(0, 3) == "-ER")
{
status += "Unable to log into server";
return;
}
writer.WriteLine("STAT"); //Send stat command to get number of messages
writer.Flush();
serverResponse = reader.ReadLine();
string[] count = serverResponse.Split(' ');
messageCount = Convert.ToInt32(count[1]);
if (messageCount > 0)
{
status += "You have " + messageCount + " message(s)";
}
else
{
status += "You have no messages";
}
for (int i = 1; i <= messageCount; i++)
{
writer.WriteLine("TOP " + i + " 0"); //read header of each message
writer.Flush();
serverResponse = reader.ReadLine();
while (true)
{
serverResponse = reader.ReadLine();
if (serverResponse == ".") break;
if (serverResponse.Length > 4)
{
if (serverResponse.Substring(0, 5) == "From:") emailFrom = serverResponse;
if (serverResponse.Substring(0, 8) == "Subject:") emailSubject = serverResponse;
}
}
messages.Items.Add(i + " " + emailFrom + " " + emailSubject);
}
}
This code doesn't throw any errors, but it seems to stop executing at the line specified.
My second attempt I tried in a web based application. I get more of a response here, but if you look at the attached screen shot you'll see what my issue is. I do a Response.Write to see what the response is (as I get an error) and the screen shot shows what I see

public string SendCommand(ref NetworkStream stream, string msg)
{
byte[] data = Encoding.ASCII.GetBytes(msg.ToCharArray());
stream.Write(data, 0, data.Length);
//Response.Write(msg);
return GetResponse(ref stream);
}
// check if there is a response to get and return it
public string GetResponse(ref NetworkStream stream)
{
byte[] bytes = new byte[client.ReceiveBufferSize + 1];
int ret = stream.Read(bytes, 0, bytes.Length);
StreamReader reader = new StreamReader(stream);
// Returns the data received
Response.Write(Encoding.ASCII.GetString(bytes));
return Encoding.ASCII.GetString(bytes);
}
public void ReadMail(string host, string user, string pass)
{
// initialise objects
NetworkStream stream;
string hostResponse;
// open connection to server
try
{
client.Connect(host, 995);
}
catch (Exception ex)
{
Response.Write(ex.Message);
Response.End();
}
// get response
stream = client.GetStream();
hostResponse = GetResponse(ref stream);
// enter user name
hostResponse = SendCommand(ref stream, "USER " + user);
// enter password
hostResponse = SendCommand(ref stream, "PASS " + pass);
// check if logged in ok
if (!(hostResponse.Substring(0,4) == "-ERR"))
{
Response.Write("Login Successful!");
}
else
{
Response.Write("Invalid login. Please try again<BR>");
Response.Write("<p>" + hostResponse + "</p>");
Response.End();
}
hostResponse = SendCommand(ref stream, "STAT");
string[] tmpArray;
tmpArray = hostResponse.Split();
string numMess = tmpArray[0];
Response.Write("<p><hr></p>");
hostResponse = "";
Response.Write("Number of Messages: " + numMess);
Response.End();
if (Convert.ToInt32(numMess) > 0)
{
Response.Write("Messages: " + numMess + "<br>");
for (int i = 1; i <= Convert.ToInt32(numMess); i++)
{
hostResponse += SendCommand(ref stream, "TOP " + i + " 10");
}
}
else
{
Response.Write("Messages: None" + "<br>");
}
hostResponse += SendCommand(ref stream, "STAT");
tmpArray = hostResponse.Split("+OK".ToCharArray());
for (int j = 1; j <= tmpArray.Length - 1; j++)
{
Response.Write("<h3>#" + j + "</h3>" + tmpArray[j] + "<br/>");
}
// close connection
hostResponse = SendCommand(ref stream, "QUIT" );
client.Close();
}
Anyone got any ideas what I'm doing wrong here?

New Topic/Question
Reply



MultiQuote







|