Home | Demo | Contact Us | Download | Support | Purchase | Products


Other Products
 
  "Sure enough. You are absolutely correct. Once I followed your solution, everything worked fine. Thanks for your quick turn around."
Eric
November Consulting.com
 
More
Testimonials

Summary

The following article will describe how to use aspNetMime from Visual Studio .NET using C#. This brief tutorial assumes you have IIS installed locally on your machine, and have downloaded and installed aspNetMime from www.aspNetMime.com . For an updated version of this article, visit www.aspNetMime.com/examples.aspx To view the entire code listing, go here.

 

As an example, this this tutorial will discuss creating an ASP.NET page that connects to a POP3 server, using aspNetPOP3 ( www.aspNetPOP3.com ), downloads a message, and displays the message contents in the ASP.NET page.

 

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/aspNetMimeTest

 

Setting the Reference to aspNetMime.

There are two ways to set a reference in VS.NET . Because aspNetMime is installed in the GAC (Global Assembly Cache) you can set a reference to the GAC copy, or you can upload the aspNetMime.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.aspNetMime.com/licen.aspx  for more information.)

1. In the Solution Explorer, right-click the project name, aspNetMimeTest, 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 aspNetMime install directory. By default, this directory is C:\Program Files\AdvancedIntellect\aspNetMime. Double-click the aspNetMime.dll. The Add Existing Item dialog box closes, and the aspNetMime.dll was uploaded to the root directory of your project.

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

4. Repeat these same steps to set a reference to the aspNetPOP3.dll. aspNetPOP3 can be downloaded from www.aspNetPOP3.com

 

Create a Test Page.

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

1. In the Solution Explorer, right-click the project name aspNetMimeTest, 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="getmessage" method="post" runat="server">
	<h3>This example uses aspNetPOP3 and aspNetMime to read the first message from a POP3 server and display it on this page.</h3>
	<table>
		<tr>
			<td>From</td>
			<td><asp:Literal ID=litFrom Runat=server></asp:Literal></td>
		</tr>
		<tr>
			<td>To</td>
			<td><asp:Literal ID=litTo Runat=server></asp:Literal></td>
		</tr>
		<tr>
			<td>Subject</td>
			<td><asp:Literal ID=litSubject Runat=server></asp:Literal></td>
		</tr>
		<tr>
			<td colspan=2>
				<pre><asp:Literal ID=litContents Runat=server></asp:Literal></pre>
			</td>
		</tr>
		<tr>
			<td>
				Attachment List
			</td>
			<td>
				<asp:Literal ID=litAttachments Runat=server></asp:Literal>
			</td>
		</tr>
	</table>
     </form>

This code creates a simple webform that contains 5 literal controls (litFrom , litTo , litSubject , litContents , litAttachments  ). The codebehind page will populate these literal controls with content retrieved from a POP3 server.
 

 

4. Select the View | Code menu command. The Codebehind (getmessage.aspx.cs) page should be visible in the editor.

5. At the top of page, add the following using commands.

using aspNetMime;
using aspNetPOP3;

6. Modify the private void Page_Load(object sender, System.EventArgs e) method to match the code found below.

		
private void Page_Load(object sender, System.EventArgs e)
{
	//create the POP3 object
	POP3 pop = new POP3( "127.0.0.1", "dave@blah.com", "mypassword" );

	//connect to the server
	pop.Connect();

	//get the message
	MimeMessage m = pop.GetMessage(0);

	//disconnect
	pop.Disconnect();

	//display the message contents 
	if( m.To != null )
		litTo.Text = Server.HtmlEncode( m.To.ToString() );

	if( m.From != null )
		litFrom.Text = Server.HtmlEncode( m.From.ToString() );

	if( m.Subject != null )
		litSubject.Text = Server.HtmlEncode( m.Subject.Value );
	else
		litSubject.Text = "(no subect)";

	if( m.MainBody != null )
		litContents.Text = m.MainBody;

	//check for attachments
	MimePartCollection attachments = m.Attachments;
	if( attachments.Count > 0 )
	{
		//if there are attachments, then write out their names
		foreach(MimePart attachment in attachments )
		{
			litAttachments.Text += attachment.AttachmentName() + "<BR>";
		}
	}
	else
	{
		litAttachments.Text = "(no attachments)";
	}

}

	

This code will connect to the POP3 server specified by POP3 pop = new POP3( "127.0.0.1", "dave@blah.com", "mypassword" );. Once connected, the pop object will retrieve the message by executing MimeMessage m = pop.GetMessage(0);, and disconnect from the server.

Once the message has been created, the litTo, litFrom, and litSubject literal tags are populated with the their respective Message headers. The litContents tag is populated with the message body. The attachment collection is checked, and if any attachments are found, their names are added to the litAttachments tag.

Testing The Page

Lets compile and test the page.

1. In the Solution Explorer, right-click the project name, aspNetMimeTest, 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 aspNetMime from Visual Studio .NET. In these few simple steps you were able to create a project, set a reference to aspNetMime, and read a simple email. For more questions or comments, feel free to write support@advancedIntellect.com  

 

 



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