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

Other Products
  "[In response to a support question] Once again, you are the best, thanks for the help. It worked!!!"
Tom ONeil


The following article will describe how to use aspNetMime from Visual Studio .NET using VB.NET. 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.



Using Visual Studio .NET (VS.NET ) We will create a single webform (getmessage.aspx) and send out 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 Basic Projects. Under Templates, select ASP.NET Web Application. In the Location textbox, enter http://localhost/aspNetMimeTestvb . 


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>
			<td><asp:Literal ID=litFrom Runat=server></asp:Literal></td>
			<td><asp:Literal ID=litTo Runat=server></asp:Literal></td>
			<td><asp:Literal ID=litSubject Runat=server></asp:Literal></td>
			<td colspan=2>
				<pre><asp:Literal ID=litContents Runat=server></asp:Literal></pre>
				Attachment List
				<asp:Literal ID=litAttachments Runat=server></asp:Literal>

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.vb) page should be visible in the editor.

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

Imports aspNetMime
Imports aspNetPOP3

6. Modify the  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load method to match the code found below.

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'create the POP3 object
        Dim pop As POP3 = New POP3("", "dave@blah.com", "mypassword")

        'connect to the server

        'get the message
        Dim m As MimeMessage = pop.GetMessage(0)


        'display the message contents 
        If Not m.To Is Nothing Then
            litTo.Text = Server.HtmlEncode(m.To.ToString())
        End If

        If Not m.From Is Nothing Then
            litFrom.Text = Server.HtmlEncode(m.From.ToString())
        End If

        If Not m.Subject Is Nothing Then
            litSubject.Text = Server.HtmlEncode(m.Subject.Value)
            litSubject.Text = "(no subect)"
        End If

        If Not m.MainBody Is Nothing Then
            litContents.Text = m.MainBody
        End If

        'check for attachments
        Dim attachments As MimePartCollection = m.Attachments
        If attachments.Count > 0 Then
            'if there are attachments, then write out their names
            Dim attachment As MimePart
            For Each attachment In attachments
                litAttachments.Text += attachment.AttachmentName() + "<BR>"
            litAttachments.Text = "(no attachments)"
        End If
    End Sub

This code will connect to the POP3 server specified by Dim pop As POP3 = New POP3("", "dave@blah.com", "mypassword"). Once connected, the pop object will retrieve the message by executing   Dim m As MimeMessage = 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.


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.