Accessing a POP3 Email Account through an ASP PageBy 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).
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
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:
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:
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
readMessage.asp, then, needs to display the body text for this email message. This is
accomplished via the following steps:
- Open a connection to the POP3 server.
- Read in the particular email, based on the passed in
- 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
The complete code for
readMessage.asp can be seen below.
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.