Thursday, April 22, 2010

POP3 (Post Office Protocol) in Asp.net to recieve email

Hi all .. let me Put Some begginging to read email from gmail using POP3.. all the details of your mail server can be get from gmail settings...
Socket programming enable us in doing this ... lets see that in simple



............. Code Goes Here...........................
...........name spaces  to be used........

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;
 
and the code in the button Event
try

{

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 get this from gmail setting

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 

//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 your_gmail_user_name@gmail.com"); // refer POP rfc command, there very few around 6-9 command

sw.Flush(); // sent to server

sw.WriteLine("PASS your_gmail_password"); 
sw.Flush();

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

sw.Flush();

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

sw.Flush();

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

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

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

{

break; 
}

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

break; 
}

str += strTemp;

}

Response.Write(str);

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

catch (Exception ex) 
{

Response.Write(ex.Message);

}

}
 

    
     

3 comments:

  1. This so good stuff .keep up the good work. I read a lot of blogs on a daily basis and for the most part just wanted to make a quick comment to say I’m glad I found your blog,dear martin.Great this is very useful to me.. hank you, especially for mentioning pop3!



    I'm new to this so I look forward to continuing this article!

    I look forward to writing my own programs one day and giving back to the internet what it has done for me!How much will it cost me to start configuring on my site?How do I authenticate to send an email in my website[not pop3,direct sending from my own email server]?

    ReplyDelete
  2. :) varun email sending protocol is SMTP (Simple Mial Transfer Protocol) and recieving protocol is POP3 (post OFfice protocol) all mail servers work with this as basic protocol

    Thank u man.. :)

    ReplyDelete
  3. Martin sir - How it works? Response.Write(str) will write first mail content in asp.net application from gmail, right.

    ReplyDelete