The following steps will demonstrate how to use aspNetMime in a non-Visual Studio .NET environment. For this ASP.NET application to work successfully you will need FTP access or file share access to your website.
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.
1. FTP (or through file sharing) connect to your web application.
2. Locate the /Bin directory. If there isn't a directory named Bin you will need to create it under the root directory.
3. Upload the aspNetMime.dll to the /Bin directory. By default, the aspNetMime.dll can be found in c:\Program Files\advancedintellect\aspNetMime.
4. Repeat these same steps to set a reference to the aspNetPOP3.dll. aspNetPOP3 can be downloaded from www.aspNetPOP3.com
Once the aspNetMime and aspNetPOP3 has been uploaded, you will be able to create a test ASP.NET page. The following steps will demonstrate this, using both C# and VB.NET.
1. To create a sample page, called GetMessage.aspx, start Notepad.
2. If you are using C# as your development language, enter the following code. If you are using VB.NET, that code can be found in the next step.
[C#]
<%@ Page language="c#" %> <%@ Import Namespace="aspNetPOP3" %> <%@ Import Namespace="aspNetMime" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>getmessage</title> </HEAD> <body > <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> </body> </HTML> <script runat=server> 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)"; } } </script>
[VB.NET]
<%@ Page language="vb" %> <%@ Import Namespace="aspNetPOP3" %> <%@ Import Namespace="aspNetMime" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>getmessage</title> </HEAD> <body > <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> </body> </HTML> <script runat=server> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) 'create the POP3 object Dim pop As POP3 = New POP3("127.0.0.1", "dave@blah.com", "mypassword") 'connect to the server pop.Connect() 'get the message Dim m As MimeMessage = pop.GetMessage(0) 'disconnect pop.Disconnect() '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) Else 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>" Next Else litAttachments.Text = "(no attachments)" End If End Sub </script>
4. This code will connect to the POP3 server specified by
[C#] POP3 pop = new POP3( "127.0.0.1", "dave@blah.com", "mypassword" );. [Visual Basic] Dim pop As POP3 = New POP3("127.0.0.1", "dave@blah.com", "mypassword") Once connected, the pop object will retrieve the message by executing [C#] MimeMessage m = pop.GetMessage(0);, and disconnect from the server. [Visual Basic] Dim m As MimeMessage = pop.GetMessage(0) 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.
POP3 pop = new POP3( "127.0.0.1", "dave@blah.com", "mypassword" );.
[Visual Basic]
Dim pop As POP3 = 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.
Dim m As MimeMessage = pop.GetMessage(0)
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.
5. Save this file as getmessage.aspx and upload it to your web application. Note: You will need to change the Server, Username and Password properties to reflect your local mail server and email addresses.
6. Open Internet Explorer (or a suitable web browser) and navigate it to your website and view the getmessage.aspx (for example http://localhost/getmessage.aspx).
7. 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 a Non - Visual Studio .NET environment. In these few simple steps you were able to create an email and send it from an ASP.NET page.
Copyright 2003 - Contact: Webmaster Last Updated: Monday, March 01, 2010