Reading eMail from Mail Server

Reading mail from a mail server is done in a few steps.
First a connection to be established into the server giving our credentials.
Then using POP3 commands we can query the server.

Establishing a connection
--------------------------


TcpClient tcpclient = new TcpClient(); // create an instance of TcpClient

tcpclient.Connect("pop.gmail.com", 995); // HOST NAME POP SERVER and gmail uses port number 995 for POP

Now a connection has been established and we are ready to query the server through the connection.

For that, we are using a Secure Cocket Layer Stream object.

System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream()); // This is Secure Stream
// opened the connection between client and POP Server

sslstream.AuthenticateAsClient("pop.gmail.com"); // authenticate as client
if (!sslstream.IsAuthenticated)
{
Response.Write("NotAuthenticated !");
Response.End();

}
Response.Write("Authenticated !



");



Now we can use a StreamReader object to read data from the stream and StreamWrite object write commands in to the stream.


System.IO.StreamReader reader = new StreamReader(sslstream); // Assigned reader to stream
System.IO.StreamWriter sw = new StreamWriter(sslstream); // Assigned writer to stream


Using the streamWriter object we can issue commands to the Server.
The 'USER' command gives access to a mail box
The Flush method is used to send the string onto the stream.


sw.WriteLine("USER " + tbUserName.Text.Trim() + "@gmail.com"); // refer POP rfc command, there very few around 6-9 command
sw.Flush();
reader.ReadLine();
sw.WriteLine("PASS " + tbPassword.Text);
sw.Flush(); // sent to server
string str = string.Empty;
string strTemp = string.Empty, strErr = string.Empty;

reader.DiscardBufferedData();

sw.WriteLine("STAT");
sw.Flush();

// strTemp = reader.ReadLine();
// Response.Write(strTemp + "
");
strTemp = reader.ReadLine();
Response.Write(strTemp + "
");
strTemp = reader.ReadLine();
Response.Write(strTemp + "
");
strTemp = reader.ReadLine();
Response.Write(strTemp + "
");

if (strTemp.IndexOf("-ERR") >= 0)
{
Response.Write("
Error !");
return;
}
int n1 = strTemp.IndexOf(' ');
int n2 = strTemp.IndexOf(' ', n1 + 1);
n2 = n2 - n1;
int n = Convert.ToInt32(strTemp.Substring(n1, n2));
if (n == 0)
{
Response.Write("
Mailbox is empty!");
Response.End();
return;
}

Response.Write("
Contents");
for (int i = 1; i <= n; i++)
{
sw.WriteLine("RETR " + i.ToString());
sw.Flush();
Response.Write("

-----------------------------------------

");
strTemp = reader.ReadLine();
str = "";
while (true)
{
if ((strTemp = reader.ReadLine()) == null) break;
if (strTemp == ".")
break;
if (strTemp.IndexOf("+OK") >= 0 || strTemp.IndexOf("-ERR") >= 0)
strErr += strTemp + "
";
else
str += strTemp + "
";
}
Response.Write(str);
}

Response.Write("


" + "---End---");
sw.WriteLine("QUIT");
sw.Flush();
reader.Close();
sw.Close();

if (strErr != string.Empty)
Response.Write("



Errors :" + strErr);
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: