Home | Contact Us | Download | Support | Purchase | Shopping Cart | Products


Product List

Summary

The following article will describe how to use aspNetPOP3 from Visual Studio .NET using C#. This brief tutorial assumes you have IIS installed locally on your machine, and have downloaded and installed aspNetPOP3 from www.aspNetPOP3.com . To view the entire source code for this tutorial, go here.

 

Instructions

Using Visual Studio .NET (VS.NET ) We will create a single webform (getmessage.aspx) to download an email.

 

1. Launch Visual Studio .NET

2. From the main menu, select the File | New | Project command.

3. The New Project dialog box appears. Under Project Types, Select Visual C# Projects. Under Templates, select ASP.NET Web Application. In the Location textbox, enter http://localhost/aspNetPOP3Test

 

Setting the Reference to aspNetPOP3.

There are two ways to set a reference in VS.NET . Because aspNetPOP3 is installed in the GAC (Global Assembly Cache) you can set a reference to the GAC copy, or you can upload the aspNetPOP3.dll to the project and set a reference to the uploaded copy. We will upload a copy, and set a reference to the uploaded copy. This method will promote XCOPY deployment. (A comment about licensing: Please be sure you are compliant with your licenses. Check http://www.aspNetPOP3.com/licen.aspx  for more information.)

1. In the Solution Explorer, right-click the project name, aspNetPOP3Test, and select the Add | Add Existing Item command.

2. The Add Existing Item dialog box appears. Under Files of Type, select All Files(*.*). Navigate to the aspNetPOP3 install directory. By default, this directory is C:\Program Files\AdvancedIntellect\aspNetPOP3. Double-click the aspNetPOP3.dll. The Add Existing Item dialog box closes, and the aspNetPOP3.dll was uploaded to the root directory of your project.

3. In the Solution Explorer, right-click the project name aspNetPOP3Test and select Add Reference. The Add Reference dialog box appears. Click the Browse button. The Select Component dialog box appears. Double-click the aspNetPOP3.dll file. The Select Component dialog box closes. Click OK. The Add Reference dialog box closes, and a reference is set to aspNetPOP3.

 

Create a Test Page.

Now that we have a reference set, let's create a test page to check aspNetPOP3.

1. In the Solution Explorer, right-click the project name aspNetPOP3Test, and select Add | Add Webform. The Add New Item dialog box appears. In the Name textbox, enter, getmessage.aspx.

2. Select the Edit | View HTML Source Menu command. The raw HTML code should now be visible.

3. Add the following HTML and ASP.NET server control syntax to getmessage.aspx.

<form id="Form1" method="post" runat="server"> 
  <table> 
    <tr> 
     <td>Enter your Server Address</td>
     <td><input type=text id="txtServer" runat=server NAME="txtServer">
<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 NAME="txtUsername">
<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 NAME="txtPassword">
<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 NAME="cmdSubmit"></td> </tr> </table> <br> <asp:literal ID=Message1 Runat=server></asp:literal> </form>

This code creates a simple webform that contains 3 text boxes (txtServer, txtUsername, txtPasswword), along with 3 validator controls. This form accepts the Mail Server address, POP3 Username, and POP3 Password
Once the form is submitted, this information will be used to retrieve the first message found at the POP3 mailserver. The message content will then be written to the <asp:literal> tag Message1.

 

 

4. Select the View | Design menu command. The HTML page should be visible in the Designer.

5. Double-Click the button labeled "Download 1st Message". The codebehind file of C# code will now load, with a method created called

private void cmdSubmit_ServerClick(object sender, System.EventArgs e)
{

}	

6. Modify the cmdSubmit_ServerClick menu match the code found below.

		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; }

This code will connect to the POP3 server specified by txtServer, txtUsername, and txtPassword. Once aspNetPOP3 connects to the server, it calls MessageCount() to see
if any messages are available. If at least 1 message is available, aspNetPOP3 calls GetMessageAsText(), to retrieve the message, and then HtmlEncodes the message, and sends the content to the Message1 <asp:literal> control.

 

Testing The Page

Lets compile and test the page.

1. In the Solution Explorer, right-click the project name, aspNetPOP3Test, and select Build. The project will be compiled.

2. In the Solution Explorer, right-click getmessage.aspx and select View in Browser.

3. Enter your Mail Server address, username and password. If at least 1 message is available at the POP3 server, it will be downloaded and displayed in the browser.

 

Summary

That's all there is to using aspNetPOP3 from Visual Studio .NET. In these few simple steps you were able to create a project, set a reference to aspNetPOP3, and read a simple email. For more questions or comments, feel free to write support@advancedIntellect.com  

 

 



The box is not shipped. aspNetPOP3 is a downloadable product.