May 21, 2012 08:20 by
Scott
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
//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:[email protected]";
objPopHelper.Password = "GmailPassword";
objPopHelper.Connect();
GridView1.DataSource = p.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);
}
}
Class Diagram of POPHelper
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;
}
Enumerates for Filer message subject and From Address and ToAddress
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;
}
Creating DataSource for GridView
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;
}
April 25, 2012 08:07 by
Scott
The above error message indicate that you haven’t configured your ASP.NET 4 on your IIS. To configure IIS7.0 to use ASP.NET 4, please follow this steps:
- Open command prompt under Administrative privileges.
- Navigate to this location C:\Windows\Microsoft.NET\Framework\v4.0.30319.
- Locate aspnet_regiis.exe file.
- Run the utility with –i switch to register ASP.NET 4.0 with IIS7
And you can see it will work now.