When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Saturday, September 22, 2001

Accessing a POP3 Email Account through an ASP Page

By Scott Mitchell

One of the most commonly asked questions on the ASPMessageboard is along the lines of, "How can I use CDONTS to receive email?" (For those of you unfamiliar with CDONTS, it is a free, lightweight email sending component that comes installed by default with Windows NT/2000; see this FAQ for more information.) The answer is that you can't use CDONTS to receive email - it is designed just to send email. If you want to receive email through an external POP3 email account, you must use a third-party component (or write your own).

- continued -

Fortunately, there are a number of third-party POP3 components. Unfortunately, none of them are free, but they do all have free evaluation periods so that you can try them out and see if they meet your needs before plunking down the $30-$150 needed for the various components. In this article we'll take a look at AspPop3 component, which carries a price tag of $69.95 after a free week-long trial. (On a side note, there is currently a free ASP.NET POP3 component available from the good folks at SoftArtisans.com.)

Downloading and Installing the AspPop3 Component
Downloading and installing the AspPop3 component is a breeze. Simply navigate on over to http://www.serverobjects.com/products.htm#AspPop3 and download the evaluation copy. This copy comes in a ZIP format. Once you've downloaded the ZIP, extract the files and copy the pop3svg.dll file to the \WinNT\system32 directory and run regsvr32 to register the component (regsvr32 pop3svg.dll). That's all there is to it! At this point, you're ready to begin using this component from an ASP page.

Connecting to a POP3 Server
Before you can use the component to download/read POP3 mail on an external Web server, you must first know the hostname or IP address of the POP3 mail server. This will likely be something like pop3.yourISP.com. Yahoo! mail provides POP3 access, for example, at the POP3 server pop.mail.yahoo.com. Along with this hostname (or IP), you need to know your username/password for accessing your POP3 account. Hopefully you know this information! :)

To connect to the POP3 server using AspPop3, simply use the following code:

'Create an instance of the AspPop3 component
Dim Mailer
Set Mailer = Server.CreateObject("POP3svg.Mailer")

'Specify the POP3 server
Mailer.RemoteHost = "pop3.someserver.com"

'Specify your username/password
Mailer.UserName   = "username"
Mailer.Password   = "password"

'Open the connection to the POP3 server

At this point, you have openned a connection to the POP3 server. Now you can download messages, retrieve messages, delete messages, etc. The official AspPop3 documentation lists all of the properties and methods of this component, so I won't go into them in detail in this article. Rather, let's examine a simple (albeit inefficient) Web page that one can use to view/read a remote POP3 account!

A Simple POP3 Reader Application
This POP3 reader application consists of two ASP pages. The first simply connects to the POP3 Web server and retrieves all of the messages, displaying the subject and who the email was sent from. A link is provided for each email message that will take the user to the second page, readMessage.asp which, as the name suggests, will display the message's body.

It is important to understand how the first page works, and its limitations. The first page simply iterates through the POP3 inbox, reading the entire message and displaying only its subject and from address. No messages are being deleted from the POP3 server, and the messages being read locally are not being persisted - that is, we are not saving the incoming POP3 messages to the hard drive (an option that AspPop3 provides). Realize that the first page is not very efficient at all, and can take quite a long time to execute for POP3 accounts that have many messages. In testing this application on my personal Yahoo! email inbox, it took roughly three seconds to download the 30-some odd messages to my local computer (which has a DSL connection). Your mileage may vary, but be ready for a lengthy wait if your POP3 inbox has hundreds (or, eep, thousands) of messages!

Our first page displays the total messages in the POP3 server and displays the subject and from fields of the emails in a nice HTML table:

  'Open a connection to the POP3 server
  Dim Mailer
  Set Mailer = Server.CreateObject("POP3svg.Mailer")
  Mailer.RemoteHost = "pop3.foo.com"
  Mailer.UserName   = "username"
  Mailer.Password   = "password"

  'Find out how many messages there are
  Dim iMessages
  iMessages = Mailer.MessageCount
  Response.Write "There are " & iMessages & " messages on the server.<p>"

  Dim iLoop

  'Display the table
  Response.Write "<table border=1 align=center cellspacing=1>"
  Response.Write "<tr><th>&nbsp;</th><th>Subject</th><th>From</th></tr>"

  'Loop through all of the available messages
  For iLoop = 1 to iMessages
    If Mailer.Retrieve(iLoop) then
      Response.Write "<tr>"
      Response.Write "<td><a href=""readMessage.asp?ID=" & _
                     iLoop & """>Read</a></td>"
      Response.Write "<td>" & Mailer.Subject & "</td>"
      Response.Write "<td>" & Mailer.FromName & " (<a href=""mailto:""" & _
                     Mailer.FromAddress & """>" & Mailer.FromAddress & _
      Response.Write "</tr>"
    End If
  Next 'iLoop

  Response.Write "</table>"

  'Close the connection to the POP3 server

A screenshot of the first page. A screenshot of the first page can be seen to the right. Note that each message has a hyperlink titled "Read" that, when clicked, will take the user to readMessage.asp, passing along the correct ID of the message. readMessage.asp, then, needs to display the body text for this email message. This is accomplished via the following steps:

  1. Open a connection to the POP3 server.
  2. Read in the particular email, based on the passed in ID.
  3. Display the message's subject and body text. (The body text's carraige returns (vbCrLf) either need to be converted into HTML line breaks (<br> tags) or the use of a <pre> tag must be used to maintain formatting... The latter approach is used in readMessage.asp.)

The complete code for readMessage.asp can be seen below.

  'Open a connection to the POP3 server
  Dim Mailer
  Set Mailer = Server.CreateObject("POP3svg.Mailer")
  Mailer.RemoteHost = "pop3.foo.com"
  Mailer.UserName   = "username"
  Mailer.Password   = "password"

  'Retrieve the specific message
  If Mailer.Retrieve(Request("ID")) Then
    Response.Write "<b>Subject</b>: " & Mailer.Subject & "<br>"
    Response.Write "<b>Body</b>: <pre>" & _
                   Server.HtmlEncode(Mailer.BodyText) & "</pre><br>"
    Response.Write "Message " & Request("ID") & " could not be found..."
  End If

  'Close the connection

That about wraps up our sample application, as well as this article! Note that accessing a POP3 inbox is quite possible through an ASP page if you have a suitable POP3 component.

Happy Programming!

  • By Scott Mitchell

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article