Dev Direct Solution Center

For more information and to buy this product...

List email messages waiting on POP server using IP*Works! ASP.NET Edition by /n Software

  For a copy of the sample project please click here to download and install the product evaluation.

Introduction

Using the POP Component in IP*Works! .NET Edition to access and list the contents of a POP3 mailbox. The sample runs from an ASP.Net based web page which requests the authentication information with a web form and displays the list of messages.

Detail

The solution shows how we can easily log in to a mailbox and retreive a list of waiting messages. The sample is ASP.Net based but could equally be adapted to run from a Winforms application.

Setup

First download the trial version of IP Works using the link above and run the installer.

Following installation you will find the sample applications in:

C:\Program Files\nsoftware\IPWorks V8 .NET Edition\demos - webform\aspx2-cs\ipwaspx.sln for C#.Net

C:\Program Files\nsoftware\IPWorks V8 .NET Edition\demos - webform\aspx2-vb\ipwaspx.sln for VB.Net

The solution contains numerous samples, this one is contained in Pop.aspx

Set this form to be the startup object and run the project. You wil see the following form. Enter the IP address or DNS name of the mail server and a valid username and password. Then hit "List Messages" to retreive a list of mail messages in the mailbox on the server.

Code Elements

The code that does the work is in the BindData method. The MailServer,User & Password properties are set with user values and then the Connect method establishes a connection and populates the MessageCount property.

A loop is then established in which the RetreiveHeader method (orRetreive see below) is called, which causes the messageSubject, MessageFrom and MessageDate fields to be populated.

C#
private void BindData() { //Define the table that will receive the messages DataTable dtMessageInfo = new DataTable(); dtMessageInfo.Columns.Add("Index", typeof(int)); dtMessageInfo.Columns.Add("Subject"); dtMessageInfo.Columns.Add("From"); dtMessageInfo.Columns.Add("Date"); //set up the logon properties pop1.MailServer = txtMailServer.Text; pop1.User = txtUser.Text; pop1.Password = txtPassword.Text; //Establish the connection pop1.Connect(); //Loop the messages in the inbox for (int i = 1; i<= pop1.MessageCount; i++) { //Get the message headers pop1.MessageNumber = i; pop1.RetrieveHeaders(); //Load the message info into the table DataRow r = dtMessageInfo.NewRow(); r["Index"] = pop1.MessageNumber; r["Subject"] = pop1.MessageSubject; r["From"] = pop1.MessageFrom; r["Date"] = pop1.MessageDate; dtMessageInfo.Rows.Add(r); } gridResults.DataSource = dtMessageInfo; gridResults.DataBind(); pop1.Disconnect(); }
VB
Private Sub BindData() Dim i As Integer 'Set up the logon properties Pop1.MailServer = txtMailServer.Text Pop1.User = txtUser.Text Pop1.Password = txtPassword.Text 'Connect to the server Pop1.Connect() 'Setup the table to receive the message data MessageInfo.Tables.Add("info") MessageInfo.Tables("info").Columns.Add("Index", System.Type.GetType("System.Int32")) MessageInfo.Tables("info").Columns.Add("Subject", System.Type.GetType("System.String")) MessageInfo.Tables("info").Columns.Add("From", System.Type.GetType("System.String")) MessageInfo.Tables("info").Columns.Add("Date", System.Type.GetType("System.String")) Pop1.MaxLines = 1 'Loop the messages in the inbox For i = 1 To Pop1.MessageCount strSubject = "" strFrom = "" strDate = "" 'Get the message Pop1.MessageNumber = i Pop1.Retrieve() Dim myDataRow As System.Data.DataRow myDataRow = MessageInfo.Tables("info").NewRow() myDataRow("Index") = Pop1.MessageNumber 'Load the table with the message header info 'strings are populated in the OnHeader event myDataRow("Subject") = strSubject myDataRow("From") = strFrom myDataRow("Date") = strDate MessageInfo.Tables("info").Rows.Add(myDataRow) Next gridResults.DataSource = MessageInfo() gridResults.DataBind() Pop1.Disconnect() End Sub

The VB sample uses a slighly different approach by retreiving the messages with the Retreive method rather than the RetreiveHeader method.

This causes the OnHeader event to be raised and the header information can be captured from the e parameter.

VB
Private Sub Pop1_OnHeader(ByVal sender As Object, ByVal e As IPWorks.PopHeaderEventArgs) Handles Pop1.OnHeader Select Case UCase(e.Field) Case "SUBJECT" : strSubject = e.Value Case "FROM" : strFrom = e.Value Case "DATE" : strDate = e.Value End Select End Sub

Conclusion

This sampe illustrates how simple it is to access a POP3 mailbox from your application using IP*Works!.Net

IP*Works! has many other features for working with the POP3 protocol and also contains a comprehensive range of components for other protocols and Internet-based capabilities.

Visit /n software inc. for more information and more samples.