AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=240&pId=-1
POP3 Email
page
by Damian Manifold
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 69055/ 114

Introduction
POP3 Email
by Damian Manifold

.NET comes with some new and interesting toys, but the one thing that is missing is the ability to receive POP3 Mail. Here we will build a class to take care of this oversight.
Logging On & Logging Off

Logging On & Logging Off
by Damian Manifold

Here is the code to connect to the server and communicate with it; also it allows you to perform the 2 most basic mail functions which are Logging On and Logging Off.

Source Code
PopMail.aspx The Form to enter the Account Details
PopMail.aspx.vb The Code to do all the work

You will notice that when responses are checked to be okay we only check for the +OK, other information maybe returned after but this is not part of the Standard and should be ignored.

Examples
PopMail.aspx the mail example in action

Getting MailBox & Message Size

Getting MailBox & Message Size
by Damian Manifold

Once you have logged in, the next easiest thing to do is to retrieve statistics about the mailbox and messages.

Below you will find the POP3 class code with 3 new properties MailBoxSize , MessageCount, and a new class MailMessage. Although at the moment it only holds the ID and size, we fill go on to add more properties as we retrieve them from the server.

Source Code
PopMail.aspx The Form to enter the Account Details
PopMail.aspx.vb The Code to do all the work

You can see in the code that the MailBoxSize and MessageCount properties have been written to set the value the first time they are called, this has been done to reduce unnecessary trafic.Your mailbox will locked once you have logged in and not released until you log out, so remember the quicker you perform your task the better. If you wish to experiment remember that if your code bombs out whilst you are logged into your mailbox it will remain locked until it times out in about 5 to 10 minutes.

So now you have the ability to add a "You've got mail!" component to you homepage, or better identify oversized emails in your mailbox.

Examples
PopMail.aspx the mail example in action

Retrieve Message and Headers

Retrieve Message and Headers
by Damian Manifold

Now we shall look into retrieving the mail message from the server, and then go on to extract the header information.

Source Code
PopMail.aspx The Form to enter the Account Details
PopMail.aspx.vb The Code to do all the work

You can see that we have now modified the MailMessage Class.

  161   public class MailMessage
  162   Inherits System.Web.Mail.MailMessage
Code Sample1 Generated using CodeView

The class now inherits from the SMTP MailMessage class. This means that it now can hold all the information of the email without us having to re-invent the wheel; it also makes it easy to pass the mail back out using the SMTP Mail class.

Now that we are going to be retrieving the entire message we can expect to receive a lot more data. The Getdata function has been enhanced to be able to read more than just a short reply.

  185  private function getData() as string
  186      Dim bData(t.ReceiveBufferSize) as byte
  187      getData = ""
  188      Do
  189          'get the data
  190          s.Read(bData, 0, bData.Length)
  191          getData += System.Text.Encoding.ASCII.GetString(bData)
  192          'clear byte array for next pass
  193          bData.Clear(bData, 0, bData.Length)
  194          'wait for the dataavailble flag to get set
  195          System.Threading.Thread.Sleep(250)
  196          'if there is more data repeat
  197      Loop while s.DataAvailable
  198  End function
Code sample2 Generated using CodeView

The message stream is read in a loop until there is no more data. There is a small delay to allow the DataAvailable flag to be set, 250 milliseconds seems more adequate, unless you have anti-virus software that scans emails, in which you may need to increase this.

There 2 new public functions in the class, getHeader and getMail. getHeader uses the TOP pop3 command to retrieve just the header component of the mail message. Where as getMail users the RETR pop3 command to retrieve the entire message and then seperates it into its Header and Body parts. Both functions use the private setHeader function to extract the header information into the MailMessage.

The setHeader function extracts the standard header information and a few of the more useful ones such as Message-ID. The mail Header and does contain a lot more information, as to whether is useful is up to you.

The example below shows the getMail function in action. You can see getHeader function in action in the next part where we go on to delete messages.

Examples
PopMail.aspx the getMail in action

Deleting Messages

Deleting Messages
by Damian Manifold

Deleting messages is a simple but important task; here you will see the GetHeader function being used to create a list of Messages to be marked for deletion by the delete function.

Source Code
PopMail.aspx The Form to enter the Account Details
PopMail.aspx.vb The Code to do all the work

The delete function uses the DELE pop3 command to delete a mail by using the message number, this may be a problem. In a web situation once the list has been produced there could be some time till a selection is made. If this is the case the message cannot be guaranteed to be in the same position, as there is the possibility that the MailBox may have be accessed by another client.

This being the case, the example uses the Message-ID header value to uniquely identify the message. It does mean however that we have to search for the corresponding message number. This is easily done in the loop that is used to produce the list, if a match is found the corresponding message is deleted and the list item struck out to indicate this.

  41  'check wether the mail has been marked for deletion
  42  if Request.Form.Item(ma(cnt).Headers("Message-ID")) <> "" Then
  43      'if it has, delete it a indicate its deletion.
  44      if p.delete(ma(cnt)) Then
  45          r.Font.Strikeout = true
  46      End if
  47  End if
Code Sample1 Generated using CodeView

Examples
PopMail.aspx the getHeader and delete functions in action

Articles and Links

Articles
Read POP3 Email with ASP.NET By Chris Garrett

Links
http://www.faqs.org/rfcs/rfc1460.html Post Office Protocol - Version 3


Product Spotlight
Product Spotlight 

©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-09-15 4:27:14 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search