A POP 3 Class
Written by Mike James   
Sunday, 14 March 2010
Article Index
A POP 3 Class
Log on and beyond
Headers
While .NET contains classes that let you send an email using SMTP, it doesn't have a standard POP class. If you need to work directly with email then POP 3 is still a reasonable choice and it's quite easy to implement your own POP 3 class. 

This project explains how POP 3 works by providing a very basic POP 3 client class. The class isn't optimised and it doesn't have error handling code but it provides a starting point for anything you want to create by way of a special POP 3 client.

POP3

The basics of POP3 are very simple.

The client connects to the server using port 110 and establishes a TCP/IP two-way communication.

It then sends various commands to the server using plain text and it receives back responses in plain text.

First the server sends an opening message something like:

+OK mailer POP3 server ready

The only thing that really matters in this message is the +OK it starts with.

All POP3 responses start with +OK if there is no error and -ERR if there is. The rest of the line is optional. Once the client receives the +OK it begins a logon procedure which goes:

USER mike
+OK mike is a valid user name
PASS secret
+OK mike is now logged on

The user name and password are transmitted without encryption and the server replies with +OK if they are recognised.

After this brief and insubstantial security check you can now start sending the server POP-3 commands to get the mail for the logged on user. You can only retrieve the mail for the logged on user so the client is going to have to repeat the logon for each mailbox.

The basic POP3 commands are

  • STAT  - server returns number of messages and total size in bytes.
  •  LIST  - lists each mail message’s size
  •  DELE n - deletes mail message n
  • NOOP - no operation used to check that the server is still alive and well
  • RETR  n - retrieve mail message n
  • TOP n m – retrieve headers and m lines of message n
  • QUIT - close down current session.

There are a few more commands, and the server might well return more than just the basic information, but these are all we really need. Putting this together a typical POP3 session might go:

<open connection>
+OK mailer POP3 server ready
USER mike
+OK
PASS mysecret
+OK mike's maildrop
has 2 messages (320 octets)
STAT
+OK 2 320
RETR 1
+OK 120 octets
the pop3 server now sends 120
characters of data that
constitutes mail message 1

.
DELE 1
+OK message 1 deleted
and so on for all the
remaining messages

QUIT

In practice you might get more in the way of a response back from the server and you might even see an error message, but that’s more or less it.

The only other thing you need to know is that each line ends with a CR+LF and the end of the mail message is marked by a line with a single dot on it.

That is, the sequence CR,LF,dot,CR,LF finishes the mail message’s text.

If you are also interested in retrieving only part of a message the command TOP n m will retrieve only the headers and the first m lines of message n. Clearly this and the DELE command can be used to manage a mailbox without having to download everything.

POP 3 in action

To see this in action we first need to create a simple POP3 class.

Start a new Windows Forms or WPF project and add a class called POP3.

We need to add a range of properties and methods to the POP3 class.

Clearly we need a URL property, to hold the URL of the post office to be contacted, and User and Password properties, all of which should be initialised to null strings when the class is created.  

public string URL { get; set; }
public string user { get; set; }
public string password { get; set; }

We also need to create a TcpClient and a NetworkStream reference to be used thoughout the project. A string to store the data retrieved as a global variable also makes things easier:

private TcpClient popTCP = 
new TcpClient();
private NetworkStream popStream;
private string reply;

To make this work we also need:

using System.Net.Sockets;

The constructor is used to set the timeouts used by the tcpClient in milliseconds:

public POP3()
{
popTCP.ReceiveTimeout = 1000;
popTCP.SendTimeout = 1000;
}

For simplicity blocking synchronous calls are used in the class an the timeouts set the maximum time that the tcpClient will wait for a response from the remote server. If the timeout is exceeded then a exception is thrown - which of course your error protected could should catch and handle. In this example, any exceptions are uncaught and simply cause the program to fail.

<ASIN:1893115402>

<ASIN:1565926285>

<ASIN:0596510292>

<ASIN:159059598X>

<ASIN:0596004710>



Last Updated ( Tuesday, 16 March 2010 )