getmessage.aspx

    

    <%@ Page language="c#" Codebehind="getmessage.aspx.cs" AutoEventWireup="false" Inherits="aspNetPOP3Test.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
    <title>Get Message</title>
</HEAD>
  <body >
	
    <form id="Form1" method="post" runat="server">
		<table>
			<tr>
				<td>Enter your Server Address</td>
				<td><input type=text id="txtServer" runat=server ><asp:RequiredFieldValidator id=RequiredFieldValidator1 runat="server" ErrorMessage="Required" ControlToValidate="txtServer"></asp:RequiredFieldValidator> </td>
			</tr>
			<tr>
				<td>Enter your username</td>
				<td><input type=text id=txtUsername runat=server><asp:RequiredFieldValidator id=RequiredFieldValidator2 runat="server" ErrorMessage="Required" ControlToValidate="txtUsername"></asp:RequiredFieldValidator></td>
			</tr>
			<tr>
				<td>Enter your password</td>
				<td><input type=password id=txtPassword runat=server><asp:RequiredFieldValidator id=RequiredFieldValidator3 runat="server" ErrorMessage="Required" ControlToValidate="txtPassword"></asp:RequiredFieldValidator></td>
			</tr>
			<tr>
				<td colspan=2 align=middle><input type=submit id=cmdSubmit value="Download 1st Message" runat=server></td>
			</tr>
		</table>
		<br>
		<asp:literal ID=Message1 Runat=server></asp:literal>
	
    </form>
	
  </body>
</HTML>

 

getmessage.aspx.cs



using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using aspNetPOP3;
namespace aspNetPOP3Test
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Literal Message1;
		protected System.Web.UI.HtmlControls.HtmlInputText txtUsername;
		protected System.Web.UI.HtmlControls.HtmlInputText txtPassword;
		protected System.Web.UI.HtmlControls.HtmlInputText txtServer;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
		protected System.Web.UI.HtmlControls.HtmlInputButton cmdSubmit;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.cmdSubmit.ServerClick += new System.EventHandler(this.cmdSubmit_ServerClick);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void cmdSubmit_ServerClick(object sender, System.EventArgs e)
		{
			//normally validate username and password. Validation left out for code simplification
			POP3 pop = new POP3( txtServer.Value, txtUsername.Value, txtPassword.Value );

			try
			{
				//connect to the server
				pop.Connect();
			}
			catch( Exception ex )
			{
				//record an exceptions and exit
				Message1.Text = "<font color=red>The following exception occurred: <br>" + ex.Message + "</font>";
				return;
			}

			int count = pop.MessageCount();
			string msg = string.Empty;
			if( count > 1 )
			{
				//get the first message
				msg = "<strong>Message 1</strong><br><br>";
				msg += "<pre>" + Server.HtmlEncode( pop.GetMessageAsText(1) ) +"</pre>";
			}
			else
			{
				//if the first message doesn't exist, write out a friendly message
				msg = "No messages at the server";
			}

			//disconnect
			pop.Disconnect();

			//show the message
			Message1.Text = msg;

		}
	}
}