Aspx file
<table border="1" style="background-color: Lime;">
<tr><td>
User Name
</td><td>
<asp:TextBox ID="txtUserName" runat="server" >
</asp:TextBox>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox>
</td>
</tr>
<tr><td>Read</td><td>
<asp:Button ID="btnHotmail" BorderWidth="0" BorderStyle="Groove" runat="server" OnClick="btnHotmail_Click" Text="Hotmail" />
<asp:Button ID="btnGmail" BorderWidth="0" BorderStyle="Groove" runat="server" OnClick="btnGmail_Click" Text="Gmail" />
</td></tr>
</table>
Code Behind File
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Socke
public void ReadMail(string hostName, int port, string userName, string password)
{
try
{
// create an instance of TcpClient
TcpClient tcpclient = new TcpClient();
// HOST NAME POP SERVER and gmail uses port number 995 for POP
tcpclient.Connect(hostName, port);
// This is Secure Stream // opened the connection between client and POP Server
System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());
// authenticate as client
sslstream.AuthenticateAsClient(hostName);
//bool flag = sslstream.IsAuthenticated;
// check flag
// Asssigned the writer to stream
System.IO.StreamWriter sw = new StreamWriter(sslstream);
// Assigned reader to stream
System.IO.StreamReader reader = new StreamReader(sslstream);
// refer POP rfc command, there very few around 6-9 command
sw.WriteLine("USER " + userName);
// sent to server
sw.Flush();
sw.WriteLine("PASS " + password);
sw.Flush();
// this will retrive your first email
sw.WriteLine("RETR 1");
sw.Flush();
// close the connection
sw.WriteLine("Quit ");
sw.Flush();
string str = string.Empty;
string strTemp = string.Empty;
while ((strTemp = reader.ReadLine()) != null)
{
// find the . character in line
if (strTemp == ".")
{
break;
}
if (strTemp.IndexOf("-ERR") != -1)
{
break;
}
str += strTemp;
}
Response.Write(str);
Response.Write("<BR>" +
"Congratulation.. ....!!! You read your first gmail email ");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
Read From Gmail
Before using this code make sure you are able to access your gmail email using outlook if not then login to www.gmail.com click on settings => pop => check to access email outside gmail web client
ReadMail("pop.gmail.com", 995, txtUserName.Text.Trim(),
txtPassword.Text.Trim());
Read From Hotmail
This article will read your first hotmail email. I have explained the commands that we can use with POP3.
I'm using ASP.net with C#.net and TcpIPClient to read email.
I saw many developers are searching for the code to read an email programatically.
ReadMail("pop3.live.com", 995, txtUserName.Text.Trim(),
txtPassword.Text.Trim());
____________________________________________________
Note* there are very few commands required to communicate with pop3 server.
You can use below commands to perform the operations on your pop3 server.
For more details about below command please refer RFC http://www.ietf.org/rfc/rfc1939.txt
1. LIST
2. RETR
3. STAT
4. USER
5. PASS
6. DELE
7. QUIT
You can use above command instead of RETR
sw.WriteLine("STAT 1");
sw.Flush();
I'm using System.Net.Security.SslStream becaus hotmail accepts secure socket.
No comments:
Post a Comment