ASP.NET Read Pop3 Email

Here is simple way of reading your gmail email using web application Before using this code make sure you are able to access your gmail email using outlook if not then login to click on settings => pop => check to access email outside gmail web client.

 Refer POP rfc

1. create new web application
2. add one button in it
3. on button click write following code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Sockets;

protected void Button9_Click(object sender, EventArgs e) 



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

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

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

sslstream.AuthenticateAsClient(""); // authenticate as client 

//bool flag = sslstream.IsAuthenticated; // check flag 

System.IO.StreamWriter sw = new StreamWriter(sslstream); // Asssigned the writer to stream

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

sw.WriteLine("USER"); // refer POP rfc command, there very few around 6-9 command

sw.Flush(); // sent to server

sw.WriteLine("PASS your_gmail_password"); 

sw.WriteLine("RETR 1"); // this will retrive your first email 


sw.WriteLine("Quit "); // close the connection


string str = string.Empty; 
string strTemp = string.Empty;

while ((strTemp = reader.ReadLine()) != null) 

if (strTemp == ".") // find the . character in line



if (strTemp.IndexOf("-ERR") != -1) 


str += strTemp;



Response.Write("<BR>" + "Congratulation.. ....!!! You read your first gmail email "); 

catch (Exception ex) 



By Sat Sat   Popularity  (18230 Views)