Satalaj More

Excellent web architect.[Working hard to make things very very simple)]
Linq to Sql

Recently started learning and sharing the experiance at

http://www.revenmerchantservices.com/
- Satalaj

asp.net code search

Nice code search
http://www.revenmerchantservices.com/ajax/

Posted: Sep 04 2008, 12:17 PM by satalaj | with no comments |
Filed under:
ASP.net Read Email

Hi,
 This is Satalaj, here is simple way of reading your gmail email using ASP.net web application
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

You can read your hotmail, if you are facing problem with gmail try below link

http://www.revenmerchantservices.com/post/2009/09/29/ASPnet-read-email-hotmail.aspx 

If you are not using gmail Secure socket layer.

try http://www.revenmerchantservices.com/page/Read-pop3-email-attachments-component.aspx

Above post will work with your company email POP3.


 Refer POP rfc  http://www.faqs.org/rfcs/rfc1939


Steps
1. create new asp.net 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)

{

try

{

// create an instance of TcpClient

TcpClient tcpclient = new TcpClient();    

// HOST NAME POP SERVER and gmail uses port number 995 for POP

tcpclient.Connect("pop.gmail.com", 995);

// 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("pop.gmail.com");

//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 your_gmail_user_name@gmail.com");

// sent to server

sw.Flush();

sw.WriteLine("PASS your_gmail_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);

}

}



 

More Posts