Read Mails from ASP.NET


This article Illustrate How to read mails from ASP.Net. Using POP commands you can access you email inbox from ASP.Net. Basic POP commands are USER, PASS, LIST, QUIT, RETR.

This article Illustrate How to read mails from ASP.Net. Using POP commands you can access you email inbox from ASP.Net. Basic POP commands are USER, PASS, LIST, QUIT, RETR.
More details POP command help you can check these links


http://www.nthelp.com/pop_commands.htm
http://www.faqs.org/rfcs/rfc1939


for Details of Class diagram Source Code and Output view of Received Mails In GridView, Check the attachment.




Implementation



//Creating Object for POPHelper
//Parameters are Gmail,Yahoo or MSN Pop Server,
//Port number
//bool isSSL
POPHelper objPopHelper = new POPHelper("pop.gmail.com", 995, true);
objPopHelper.UserName = "Your Gmail Username eg:youremail@gmail.com";
objPopHelper.Password = "GmailPassword";
objPopHelper.Connect();
GridView1.DataSource = objPopHelper.DataSource;
GridView1.DataBind();


Code Of Connect Method


public void Connect()
{
string response = string.Empty;
ArrayList arrList = new ArrayList();
try
{
//Connect to Host server
#region Connect Host
TcpClient _tcpClient = new TcpClient();
try
{
_tcpClient.Connect(_hostname, _port);
//if login is ssl
if (_isSsl)
{
_stream = new SslStream(_tcpClient.GetStream());
((SslStream)_stream).AuthenticateAsClient(_hostname);
}
else
{
_stream = _tcpClient.GetStream();
}
}
catch (Exception ex)
{
throw new POPCommandException("Connection to " + _hostname + " Port: " + _port + " failed. Error Details"+ex.Message);
}
#endregion
// Send POP Commands (USER, PASS, LIST) to Host
#region POP Commands
_streamWriter = new StreamWriter(_stream, Encoding.ASCII);
_streamReader = new StreamReader(_stream, Encoding.ASCII);

//POP command for send Username
_streamWriter.WriteLine(POPCommands.USER.ToString()+" "+ UserName);
//send to server
_streamWriter.Flush();

//POP command for send Password
_streamWriter.WriteLine(POPCommands.PASS.ToString() + " " + Password);
//send to server
_streamWriter.Flush();

//POP command for List mails
_streamWriter.WriteLine(POPCommands.LIST.ToString());
//send to server
_streamWriter.Flush();
#endregion
//Read Response Stream from Host
#region Read Response Srteam
//Read Response Stream
response = null;
string resText = string.Empty;
while ((resText = _streamReader.ReadLine()) != null)
{
if (resText == ".")
{ break; }
if (resText.IndexOf("-ERR") != -1)
{ break; }
response += resText;
arrList.Add(resText);
}
#endregion
//Binding Properties
#region Bindings
//Bind Message count
BindMailCount(arrList);
//mails returns List
_mail = ReadMail(messagecount);
//get mails Subjects returns List
_mailsub = FilterContent(_mail,FiltersOption.Subject);
_from = FilterContent(_mail, FiltersOption.From);
_to = FilterContent(_mail, FiltersOption.To);
SetDataSource(_mailsub, _from);
#endregion
}
catch (Exception ex)
{
errors.Add(ex.Message);
}
}



RETR Command is user to retrieve Mails from inbox

Reading Mails Using POP Command RETR from ASP.NET


private List ReadMail(int Count)
{
List lst = new List();
try
{
for (int i = 1; i <= Count; i++)
{
_streamWriter.WriteLine(POPCommands.RETR+" " + i.ToString());
_streamWriter.Flush();
string resText = string.Empty;
while ((resText = _streamReader.ReadLine()) != null)
{
if (resText == ".")
{ break; }
if (resText.IndexOf("-ERR") != -1)
{ break; }
lst.Add(resText);
}
}

}
catch(Exception ex)
{
errors.Add(ex.Message);
}
return lst;
}


Method for Filer mail Content

Method for Filer Content



private List FilterContent(List Mails,FiltersOption filter)
{
List filterItems = new List();
try
{
for (int i = 0; i < Mails.Count; i++)
{
if (Mails[i].StartsWith(filter.ToString() + ":"))
{
string sub = Mails[i].Replace(filter.ToString() + ":", "");
filterItems.Add(sub);
}
}
}
catch (Exception ex)
{
errors.Add(ex.Message);
}
return filterItems;
}


Set property For Email DataSource is datasource is bind to GridView or DataList


private DataTable SetDataSource(Listsubject,Listsender)
{
int messageCount = messagecount;
dataTab = new DataTable();
DataRow drow;
DataColumn Sender = new DataColumn("Sender", typeof(string));
DataColumn Subject = new DataColumn("Subject", typeof(string));
dataTab.Columns.Add(Sender);
dataTab.Columns.Add(Subject);
for (int i = 0; i < subject.Count; i++)
{
drow = dataTab.NewRow();
dataTab.Rows.Add(drow);
dataTab.Rows[i][Sender] = sender[i].ToString();
dataTab.Rows[i][Subject] = subject[i].ToString();
}
return dataTab;
}


Attachments

  • Read Email Using POP Commands from ASP.Net (38003-91048-ReadEmailUsingPOPCommands.rar)
  • Comments

    Guest Author: Bhushan03 Apr 2012

    I can not get entire message except 'OK' response



  • 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: