Sample Image - maximum width is 600 pixels

Sample Image - maximum width is 600 pixels

Sample Image - maximum width is 600 pixels

Introduction

This is a protocol (a set of commands) used by an email client to connect to and retrieve email(s) from the mailbox on the remote server. We will develop the code that connects to the POP3 server and sends commands to retrieve the list of emails on the server, and then later, using an ASP.NET page, display the list to the user. To demonstrate the code, we will be connecting to Gmail's POP3 server using SSL (Secure Sockets Layer). To connect to your own POP3 mail server, you can either use the secure connection (SSL) if your mail server supports it or connect in unsecure mode. The code is simple to understand and by looking at it you will learn a lot about how network programming is done in the .NET environment.

Basic POP3 Commands

Following is a list of commonly used POP3 commands:

  • USER - Takes one argument i.e., the email address of the user trying to connect to his/her mailbox. Example usage:
    USER youremail@xyz.com
  • PASS - Takes one argument i.e., the password of the user. Example usage:
    PASS yourpassword
  • STAT - Returns the number of emails in the mailbox and the number of bytes all the emails are taking on the server. Example usage:
    STAT
  • TOP - Takes two arguments i.e., the sort number of the email on the server and the number of lines of text to retrieve from the body of the email. Example usage:
    TOP 1 10
  • RETR - Takes one argument i.e., the sort number of the email on the server and returns all the headers and lines from the body of the email. Example usage:
    RETR 1
  • DELE - Takes one argument i.e., the sort number of the email on the server and deletes it. Example usage:
    DELE 1
  • RSET - Resets any DELE commands given above. The emails marked to be deleted by DELE command are unmarked. Example usage:
    RSET
  • QUIT - Closes the user session with the server. Example usage:
    QUIT

Steps

Create a new WebPage in VS 2005.

Step 1

Create a Class file named as Pop3.cs.

Paste this code into Pop3.cs:

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;  using System.Xml;
namespace Prabhu
{
	public class Pop3Client : IDisposable
	{
		public string Host { get; protected set; }
		public int Port { get; protected set; }
		public string Email { get; protected set; }
		public string Password { get; protected set; }
		public bool IsSecure { get; protected set; }
		public TcpClient Client { get; protected set; }
		public Stream ClientStream { get; protected set; }
		public StreamWriter Writer { get; protected set; }
		public StreamReader Reader { get; protected set; }
		private bool disposed = false;
		public Pop3Client(string host, int port, string email,
		string password): this(host, port, email, password, false){}
		public Pop3Client(string host, int port, string email,
		string password, bool secure)
		{
			Host = host;
			Port = port;
			Email = email;
			Password = password;
			IsSecure = secure;
		}
		public void Connect()
		{
			if (Client == null)
				Client = new TcpClient();
			if (!Client.Connected)
				Client.Connect(Host, Port);
			if (IsSecure)
			{
				SslStream secureStream =
				new SslStream(Client.GetStream());
				secureStream.AuthenticateAsClient(Host);
				ClientStream = secureStream;
				secureStream = null;
			}
			else
				ClientStream = Client.GetStream();
			Writer = new StreamWriter(ClientStream);
			Reader = new StreamReader(ClientStream);
			ReadLine();
			Login();
		}
		public int GetEmailCount()
		{
			int count = 0;
			string response = SendCommand("STAT");
			if (IsResponseOk(response))
			{
				string[] arr = response.Substring(4).Split(' ');
				count = Convert.ToInt32(arr[0]);
			}
			else
				count = -1;
			return count;
		}
		public Email FetchEmail(int emailId)
		{
			if (IsResponseOk(SendCommand("TOP " + emailId + " 0")))
				return new Email(ReadLines());
			else
				return null;
		}
		public List<Email> FetchEmailList(int start, int count)
		{
			List<Email> emails = new List<Email>(count);
			for (int i = start; i < (start + count); i++)
			{
				Email email = FetchEmail(i);
				if (email != null)
					emails.Add(email);
			}
			return emails;
		}
		public List<MessagePart> FetchMessageParts(int emailId)
		{
			if (IsResponseOk(SendCommand("RETR " + emailId)))
				return Util.ParseMessageParts(ReadLines());
			return null;
		}
		public void Close()
		{
			if (Client != null)
			{
				if (Client.Connected)
					Logout();
				Client.Close();
				Client = null;
			}
			if (ClientStream != null)
			{
				ClientStream.Close();
				ClientStream = null;
			}
			if (Writer != null)
			{
				Writer.Close();
				Writer = null;
			}
			if (Reader != null)
			{
				Reader.Close();
				Reader = null;
			}
			disposed = true;
		}
		public void Dispose()
		{
			if (!disposed)
				Close();
		}
		protected void Login()
		{
			if (!IsResponseOk(SendCommand("USER " + Email))
			|| !IsResponseOk(SendCommand("PASS " + Password)))
				throw new Exception("User/password not accepted");
		}
		protected void Logout()
		{
			SendCommand("RSET");
		}
		protected string SendCommand(string cmdtext)
		{
			Writer.WriteLine(cmdtext);
			Writer.Flush();
			return ReadLine();
		}
		protected string ReadLine()
		{
			return Reader.ReadLine() + "\r\n";
		}
		protected string ReadLines()
		{
			StringBuilder b = new StringBuilder();
			while (true)
			{
				string temp = ReadLine();
				if (temp == ".\r\n" || temp.IndexOf("-ERR") != -1)
					break;
					b.Append(temp);
			}
			return b.ToString();
		}
		protected static bool IsResponseOk(string response)
		{
			if (response.StartsWith("+OK"))
				return true;
			if (response.StartsWith("-ERR"))
				return false;
			throw new Exception("Cannot understand server response: " +
						response);
		}
	}
	public class Email
	{
		public NameValueCollection Headers { get; protected set; }
		public string ContentType { get; protected set; }
		public DateTime UtcDateTime { get; protected set; }
		public string From { get; protected set; }
		public string To { get; protected set; }
		public string Subject { get; protected set; }
		public Email(string emailText)
		{
			Headers = Util.ParseHeaders(emailText);
			ContentType = Headers["Content-Type"];
			From = Headers["From"];
			To = Headers["To"];
			Subject = Headers["Subject"];
			if (Headers["Date"] != null)
				try
				{
					UtcDateTime =Util.ConvertStrToUtcDateTime
							(Headers["Date"]);
				}
				catch (FormatException)
				{
					UtcDateTime = DateTime.MinValue;
				}
			else
				UtcDateTime = DateTime.MinValue;
		}
	}
	public class MessagePart
	{
		public NameValueCollection Headers { get; protected set; }
		public string ContentType { get; protected set; }
		public string MessageText { get; protected set; }
		public MessagePart(NameValueCollection headers, string messageText)
		{
			Headers = headers;
			ContentType = Headers["Content-Type"];
			MessageText = messageText;
		}
	}
	public class Util
	{
		protected static Regex BoundaryRegex = new Regex("Content-Type:
		multipart(?:/\\S+;)" + "\\s+[^\r\n]*boundary=\"?
		(?<boundary>" + "[^\"\r\n]+)\"?\r\n",
		RegexOptions.IgnoreCase | RegexOptions.Compiled);
		protected static Regex UtcDateTimeRegex = new Regex(@"^(?:\w+,\s+)?
		(?<day>\d+)\s+(?<month>\w+)\s+(?<year>\d+)\s+
		(?<hour>\d{1,2})" + @":(?<minute>\d{1,2}):
		(?<second>\d{1,2})\s+(?<offsetsign>\-|\+)
		(?<offsethours>" + @"\d{2,2})(?<offsetminutes>
		\d{2,2})(?:.*)$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
		public static NameValueCollection ParseHeaders(string headerText)
		{
			NameValueCollection headers = new NameValueCollection();
			StringReader reader = new StringReader(headerText);
			string line;
			string headerName = null, headerValue;
			int colonIndx;
			while ((line = reader.ReadLine()) != null)
			{
				if (line == "")
				break;
				if (Char.IsLetterOrDigit(line[0]) &&
					(colonIndx = line.IndexOf(':')) != -1)
				{
					headerName = line.Substring(0, colonIndx);
					headerValue = line.Substring
						(colonIndx + 1).Trim();
					headers.Add(headerName, headerValue);
				}
				else if (headerName != null)
					headers[headerName] += " " + line.Trim();
				else
					throw new FormatException
					("Could not parse headers");
			}
			return headers;
		}
		public static List<MessagePart> ParseMessageParts(string emailText)
		{
			List<MessagePart> messageParts = new List<MessagePart>();
			int newLinesIndx = emailText.IndexOf("\r\n\r\n");
			Match m = BoundaryRegex.Match(emailText);
			if (m.Index < emailText.IndexOf("\r\n\r\n") && m.Success)
			{
				string boundary = m.Groups["boundary"].Value;
				string startingBoundary = "\r\n--" + boundary;
				int startingBoundaryIndx = -1;
				while (true)
				{
					if (startingBoundaryIndx == -1)
						startingBoundaryIndx =
						emailText.IndexOf(startingBoundary);
					if (startingBoundaryIndx != -1)
					{
						int nextBoundaryIndx =
						emailText.IndexOf
						(startingBoundary,
						startingBoundaryIndx +
							startingBoundary.Length);
						if (nextBoundaryIndx != -1 &&
						nextBoundaryIndx !=
							startingBoundaryIndx)
						{
							string multipartMsg =
							emailText.Substring
							(startingBoundaryIndx +
							startingBoundary.Length,
							(nextBoundaryIndx -
							startingBoundaryIndx -
							startingBoundary.Length));
							int headersIndx =
							multipartMsg.IndexOf
							("\r\n\r\n");
							if (headersIndx == -1)
								throw new
								FormatException
								("Incompatible

							multipart message format");
							string bodyText =
							multipartMsg.Substring
							(headersIndx).Trim();
							NameValueCollection
							headers =
							Util.ParseHeaders
							(multipartMsg.Trim());
							messageParts.Add
							(new MessagePart
							(headers, bodyText));
						}
						else
							break;
						startingBoundaryIndx =
							nextBoundaryIndx;
					}
					else
						break;
				}
				if (newLinesIndx != -1)
				{
					string emailBodyText =
					emailText.Substring(newLinesIndx + 1);
				}
			}
			else
			{
				int headersIndx = emailText.IndexOf("\r\n\r\n");
				if (headersIndx == -1)
				throw new FormatException("Incompatible multipart
						message format");
				string bodyText = emailText.Substring
						(headersIndx).Trim();
				NameValueCollection headers =
					Util.ParseHeaders(emailText);
				messageParts.Add(new MessagePart(headers, bodyText));
			}
			return messageParts;
		}
		public static DateTime ConvertStrToUtcDateTime(string str)
		{
			Match m = UtcDateTimeRegex.Match(str);
			int day, month, year, hour, min, sec;
			if (m.Success)
			{
				day = Convert.ToInt32(m.Groups["day"].Value);
				year = Convert.ToInt32(m.Groups["year"].Value);
				hour = Convert.ToInt32(m.Groups["hour"].Value);
				min = Convert.ToInt32(m.Groups["minute"].Value);
				sec = Convert.ToInt32(m.Groups["second"].Value);
				switch (m.Groups["month"].Value)
				{
					case "Jan":
						month = 1;
						break;
					case "Feb":
						month = 2;
						break;
					case "Mar":
						month = 3;
						break;
					case "Apr":
						month = 4;
						break;
					case "May":
						month = 5;
						break;
					case "Jun":
						month = 6;
						break;
					case "Jul":
						month = 7;
						break;
					case "Aug":
						month = 8;
						break;
					case "Sep":
						month = 9;
						break;
					case "Oct":
						month = 10;
						break;
					case "Nov":
						month = 11;
						break;
					case "Dec":
						month = 12;
						break;
					default:
						throw new FormatException
							("Unknown month.");
				}
				string offsetSign = m.Groups["offsetsign"].Value;
				int offsetHours = Convert.ToInt32(m.Groups
						["offsethours"].Value);
				int offsetMinutes = Convert.ToInt32(m.Groups
						["offsetminutes"].Value);
				DateTime dt = new DateTime(year, month, day,
						hour, min, sec);
				if (offsetSign == "+")
				{
					dt.AddHours(offsetHours);
					dt.AddMinutes(offsetMinutes);
				}
				else if (offsetSign == "-")
				{
					dt.AddHours(-offsetHours);
					dt.AddMinutes(-offsetMinutes);
				}
				return dt;
			}
			throw new FormatException
			("Incompatible date/time string format");
		}
	}
}

Step 2

Add a WebPage named as Home.aspx.

Paste this Designing Page into Home.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{
width: 50%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td colspan="3">
<h2 style="text-align: center">Welcome To Gmail</h2>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Gmail ID :">
</asp:Label>
</td>
<td>
<asp:TextBox ID="txt_email" runat="server" Width="241px">
</asp:TextBox>
</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txt_email" ErrorMessage="Invalid Mail Format"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Password :"></asp:Label>
</td>
<td>
<asp:TextBox ID="txt_password" runat="server" TextMode="Password" Width="239px">
</asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txt_password" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>                  <td colspan="3" style="text-align: center">
<asp:Button ID="Button1" runat="server" CausesValidation="False"
onclick="Button1_Click" style="text-align: center" Text="Login" Width="72px" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>  

Paste this code into Home.aspx.cs:

using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Home : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
	}
	protected void Button1_Click(object sender, EventArgs e)
	{
		Session["email"] = txt_email.Text;
		Session["pwd"] = txt_password.Text;
		Response.Redirect("Pop3Client.aspx");
	}
} 

Step 3

Add a WebPage named as Pop3Client.aspx.

Paste this Designing Page into Pop3Client.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Pop3Client.aspx.cs"
Inherits="Pop3Client" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">  <head id="Head1" runat="server">
<title></title>
<style type="text/css">
.emails-table { width: 600px; border: solid 1px #444444; }
.emails-table-header { font-family: "Trebuchet MS"; font-size: 9pt;
background-color: #0099B9; color: white;
border: solid 1px #444444; }
.emails-table-header-cell { font-family: "Georgia"; font-size: 9pt;
font-weight: bold; border: solid 1px #666666; padding: 6px; }
.emails-table-cell { font-family: "Georgia"; font-size: 9pt;
border: solid 1px #666666; padding: 6px; }
.emails-table-footer { border: solid 1px #666666; padding: 3px;
width: 50%; }
.email-datetime { float: right; color: #666666; }
a { font-family: "Lucida Sans Unicode", "Trebuchet MS"; font-size: 9pt;
color: #005B7F; }
a:hover { color:red; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="EmailsTable" CssClass="emails-table" runat="server">
<asp:TableHeaderRow>
<asp:TableHeaderCell CssClass="emails-table-header"
ColumnSpan="4">Listing emails
<asp:Literal ID="EmailFromLiteral" runat="server" />-
<asp:Literal ID="EmailToLiteral" runat="server" /> of
<asp:Literal ID="EmailTotalLiteral" runat="server" /> for
<asp:Literal ID="EmailLiteral" runat="server" />
</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell CssClass="emails-table-header-cell">
#</asp:TableCell>
<asp:TableCell CssClass="emails-table-header-cell">
From</asp:TableCell>
<asp:TableCell CssClass="emails-table-header-cell">
Subject</asp:TableCell>
<asp:TableCell CssClass="emails-table-header-cell">
Date & Time</asp:TableCell>
</asp:TableRow>
<asp:TableFooterRow>
<asp:TableCell CssClass="emails-table-footer" ColumnSpan="4">
<asp:Table ID="FooterTable" Width="100%"
BorderWidth="0" runat="server">
<asp:TableRow>
<asp:TableCell>
<asp:Literal ID="PreviousPageLiteral" runat="server" />
</asp:TableCell>
<asp:TableCell HorizontalAlign="Right">
<asp:Literal ID="NextPageLiteral" runat="server" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:TableCell>
</asp:TableFooterRow>
</asp:Table>
</div>
</form>
</body>
</html>

Paste this coding into Pop3Client.aspx.cs:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Prabhu;
public partial class Pop3Client : System.Web.UI.Page
{
	public const string Host = "pop.gmail.com";
	public const int Port = 995;
	public string Email ;
	public string Password ;
	public const int NoOfEmailsPerPage = 5;
	public const string SelfLink =
	"<a href="http://www.codeproject.com/%22Pop3Client.aspx?page={0}\"">{1}</a>";
	public const string DisplayEmailLink =
	"<a href="http://www.codeproject.com/%22DisplayPop3Email.aspx?
		emailId={0}\"">{1}</a>";
	protected void Page_Load(object sender, EventArgs e)
	{
		int page = 1;
		if (Request.QueryString["page"] == null)
		{
			Response.Redirect("Pop3Client.aspx?page=1");
			Response.Flush();
			Response.End();
		}
		else
			page = Convert.ToInt32(Request.QueryString["page"]);
		try
		{
			Email = Session["email"].ToString();
			Password = Session["pwd"].ToString();
		}
		catch (Exception ex) { Response.Redirect("Home.aspx"); }
		int totalEmails;
		List<email /> emails;
		string emailAddress;
		using (Prabhu.Pop3Client client = new Prabhu.Pop3Client
			(Host, Port, Email, Password, true))
		{
			emailAddress = client.Email;
			client.Connect();
			totalEmails = client.GetEmailCount();
			emails = client.FetchEmailList(((page - 1) *
				NoOfEmailsPerPage) + 1, NoOfEmailsPerPage);
		}
		int totalPages;
		int mod = totalEmails % NoOfEmailsPerPage;
		if (mod == 0)
			totalPages = totalEmails / NoOfEmailsPerPage;
		else
			totalPages = ((totalEmails - mod) / NoOfEmailsPerPage) + 1;
		for (int i = 0; i < emails.Count; i++)
		{
			Email email = emails[i];
			int emailId = ((page - 1) * NoOfEmailsPerPage) + i + 1;
			TableCell noCell = new TableCell();
			noCell.CssClass = "emails-table-cell";
			noCell.Text = Convert.ToString(emailId);
			TableCell fromCell = new TableCell();
			fromCell.CssClass = "emails-table-cell";
			fromCell.Text = email.From;
			TableCell subjectCell = new TableCell();
			subjectCell.CssClass = "emails-table-cell";
			subjectCell.Style["width"] = "300px";
			subjectCell.Text = String.Format(DisplayEmailLink,
						emailId, email.Subject);
			TableCell dateCell = new TableCell();
			dateCell.CssClass = "emails-table-cell";
			if (email.UtcDateTime != DateTime.MinValue)
				dateCell.Text = email.UtcDateTime.ToString();
			TableRow emailRow = new TableRow();
			emailRow.Cells.Add(noCell);
			emailRow.Cells.Add(fromCell);
			emailRow.Cells.Add(subjectCell);
			emailRow.Cells.Add(dateCell);
			EmailsTable.Rows.AddAt(2 + i, emailRow);
		}
		if (totalPages > 1)
		{
			if (page >

Step 4

Add a WebPage named as DisplayPop3Email.aspx.

Paste this Designing Page into DisplayPop3Email.aspx:

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="DisplayPop3Email.aspx.cs" Inherits="DisplayPop3Email" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
.emails-table { width: 600px; border: solid 1px #444444; }
.emails-table-header { font-family: "Trebuchet MS"; font-size: 9pt;
background-color: #0099B9; font-weight: bold; color: white;
text-align: center; border: solid 1px #444444; }
.emails-table-header-cell { font-family: "Georgia"; font-size: 9pt;
font-weight: bold; border: solid 1px #666666; padding: 6px; }
.emails-table-cell { font-family: "Georgia"; font-size: 9pt;
border: solid 1px #666666; padding: 6px; }
.emails-table-footer { border: solid 1px #666666; padding: 3px;
width: 50%; }              .email-datetime { float: right; color: #666666; }
a { font-family: "Lucida Sans Unicode", "Trebuchet MS"; font-size: 9pt;
color: #005B7F; }              a:hover { color:red; }
pre { font-family: "Georgia"; font-size: 9pt; }
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:Literal ID="DebugLiteral" runat="server" />
<table class="emails-table">
<tr>
<td class="emails-table-header" colspan="2">
Email #<asp:Literal ID="EmailIdLiteral" runat="server" /></td>
</tr>
<tr>
<td class="emails-table-header-cell">Date & Time</td>
<td class="emails-table-cell">
<asp:Literal ID="DateLiteral" runat="server" /></td>
</tr>
<tr>
<td class="emails-table-header-cell">From</td>
<td class="emails-table-cell">
<asp:Literal ID="FromLiteral" runat="server" /></td>
</tr>
<tr>
<td class="emails-table-header-cell">Subject</td>
<td class="emails-table-cell">
<asp:Literal ID="SubjectLiteral" runat="server" /></td>
</tr>      <tr id="AttachementsRow" runat="server">
<td class="emails-table-header-cell">Attachments</td>
<td class="emails-table-cell">
<asp:Literal ID="AttachmentsLiteral" runat="server" /></td>
</tr>       <tr>
<td class="emails-table-cell" colspan="2">
<asp:Literal ID="HeadersLiteral" runat="server" /></td>
</tr>      <tr>
<td class="emails-table-cell" colspan="2">
<asp:Literal ID="BodyLiteral" runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>

Paste this code into DisplayPop3Email.aspx.cs:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Prabhu;
using System.Text.RegularExpressions;
using System.Text;

public partial class DisplayPop3Email : System.Web.UI.Page
{
	public const string Host = "pop.gmail.com";

	public const int Port = 995;

	public string Email ;

	public string Password;

	protected static Regex CharsetRegex = new Regex("charset=\"?
	(?<charset>[^\\s\"]+)\"?", RegexOptions.IgnoreCase |
	RegexOptions.Compiled);

	protected static Regex QuotedPrintableRegex =
	new Regex("=(?<hexchars>[0-9a-fA-F]{2,2})",
	RegexOptions.IgnoreCase | RegexOptions.Compiled);

	protected static Regex UrlRegex = new Regex("(?<url>https?://[^\\s\"]+)",
	RegexOptions.IgnoreCase | RegexOptions.Compiled);

	protected static Regex FilenameRegex =
	new Regex("filename=\"?(?<filename>[^\\s\"]+)\"?",
	RegexOptions.IgnoreCase | RegexOptions.Compiled);

	protected static Regex NameRegex =
	new Regex("name=\"?(?<filename>[^\\s\"]+)\"?",
	RegexOptions.IgnoreCase | RegexOptions.Compiled);

	protected void Page_Load(object sender, EventArgs e)
	{
		int emailId = -1;

		if (Request.QueryString["emailId"] == null)
		{
			Response.Redirect("Pop3Client.aspx");
			Response.Flush();
			Response.End();
		}
		else
			Email = Session["email"].ToString();
		Password = Session["pwd"].ToString();
		emailId = Convert.ToInt32(Request.QueryString["emailId"]);
		Email email = null;
		List<MessagePart> msgParts = null;
		using (Prabhu.Pop3Client client =
		new Prabhu.Pop3Client (Host, Port,Email, Password, true))
		{
			client.Connect();
			email = client.FetchEmail(emailId);
			msgParts = client.FetchMessageParts(emailId);
		}

		if (email == null || msgParts == null)
		{
			Response.Redirect("Pop3Client.aspx");
			Response.Flush();
			Response.End();
		}
		MessagePart preferredMsgPart = FindMessagePart(msgParts,
                    "text/html");

		if (preferredMsgPart == null)
			preferredMsgPart = FindMessagePart(msgParts,
                "text/plain");

		else if (preferredMsgPart == null && msgParts.Count > 0)
			preferredMsgPart = msgParts[0];
		string contentType, charset, contentTransferEncoding, body = null;
		if (preferredMsgPart != null)
		{
			contentType = preferredMsgPart.Headers["Content-Type"];
			charset = "us-ascii";
			contentTransferEncoding =preferredMsgPart.Headers
				["Content-Transfer-Encoding"];
			Match m = CharsetRegex.Match(contentType);
			if (m.Success)
				charset = m.Groups["charset"].Value;
			HeadersLiteral.Text = contentType != null
			? "Content-Type: " +contentType +
			"<br />" : string.Empty;
			HeadersLiteral.Text += contentTransferEncoding !=
			null ?"Content-Transfer-Encoding: " +
				contentTransferEncoding : string.Empty;
			if (contentTransferEncoding != null)
			{
				if (contentTransferEncoding.ToLower()== "base64")
					body = DecodeBase64String(charset,
						preferredMsgPart.MessageText);
				else
					if (contentTransferEncoding.ToLower() ==
						"quoted-printable")
						body = DecodeQuotedPrintableString
						(preferredMsgPart.MessageText);
					else
						body = preferredMsgPart.MessageText;
			}
			else
				body = preferredMsgPart.MessageText;
		}
		EmailIdLiteral.Text = Convert.ToString(emailId);
		DateLiteral.Text = email.UtcDateTime.ToString(); ;
		FromLiteral.Text = email.From;
		SubjectLiteral.Text = email.Subject;
		BodyLiteral.Text = preferredMsgPart != null ?
		(preferredMsgPart.Headers["Content-Type"].IndexOf("text/plain")
		!= -1 ?"<pre>" + FormatUrls(body) + "</pre>" : body) : null;
		ListAttachments(msgParts);
	}
	protected Decoder GetDecoder(string charset)
	{
		Decoder decoder;
		switch (charset.ToLower())
		{
			case "utf-7":
			decoder = Encoding.UTF7.GetDecoder();
			break;
			case "utf-8":
			decoder = Encoding.UTF8.GetDecoder();
			break;
			case "us-ascii":
			decoder = Encoding.ASCII.GetDecoder();
			break;
			case "iso-8859-1":
			decoder = Encoding.ASCII.GetDecoder();
			break;
			default:
			decoder = Encoding.ASCII.GetDecoder();
			break;
		}
		return decoder;
	}
	protected string DecodeBase64String(string charset, string encodedString)
	{
		Decoder decoder = GetDecoder(charset);
		byte[] buffer = Convert.FromBase64String(encodedString);
		char[] chararr = new char[decoder.GetCharCount(buffer,0, buffer.Length)];
		decoder.GetChars(buffer, 0, buffer.Length, chararr, 0);
		return new string(chararr);
	}
	protected string DecodeQuotedPrintableString(string encodedString)
	{
		StringBuilder b = new StringBuilder();
		int startIndx = 0;
		MatchCollection matches = QuotedPrintableRegex.Matches(encodedString);
		for (int i = 0; i < matches.Count; i++)
		{
			Match m = matches[i];
			string hexchars = m.Groups["hexchars"].Value;
			int charcode = Convert.ToInt32(hexchars, 16);
			char c = (char)charcode;
			if (m.Index > 0)
			b.Append(encodedString.Substring(startIndx,
					(m.Index - startIndx)));
			b.Append(c);
			startIndx = m.Index + 3;
		}
		if (startIndx < encodedString.Length)
			b.Append(encodedString.Substring(startIndx));
		return Regex.Replace(b.ToString(),
		"=\r\n", "");
	}
	protected void ListAttachments(List<MessagePart> msgParts)
	{
		bool attachmentsFound = false;
		StringBuilder b = new StringBuilder();
		b.Append("<ol>");
		foreach (MessagePart p in msgParts)
		{
			string contentType = p.Headers["Content-Type"];
			string contentDisposition = p.Headers["Content-Disposition"];
			Match m;
			if (contentDisposition != null)
			{
				m = FilenameRegex.Match(contentDisposition);
				if (m.Success)
				{
					attachmentsFound = true;
					b.Append("<li>").Append(m.Groups
					["filename"].Value).Append("</li>");
				}
			}
			else if (contentType != null)
			{
				m = NameRegex.Match(contentType);
				if (m.Success)
				{
					attachmentsFound = true;
					b.Append("<li>").Append(m.Groups
					["filename"].Value).Append("</li>");
				}
			}
		}
		b.Append("</ol>");
		if (attachmentsFound)
			AttachmentsLiteral.Text = b.ToString();
		else
			AttachementsRow.Visible = false;
	}
	protected MessagePart FindMessagePart(List<MessagePart> msgParts,
			string contentType)
	{
		foreach (MessagePart p in msgParts)
		if (p.ContentType != null && p.ContentType.IndexOf(contentType) != -1)
			return p;
		return null;
	}
	protected string FormatUrls(string plainText)
	{
		string replacementLink = "<a href=\"${url}\">${url}</a>";
		return UrlRegex.Replace(plainText, replacementLink);
	}
}

History

  • 27th April, 2011: Initial version
推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"