Email to SMS
Written by Harry Fairhead   
Monday, 27 July 2009
Article Index
Email to SMS
POP3
You have mail
Reading the mail
A Skype class

POP3

The first thing we need is a way of picking up email.

The POP3 protocol is used by nearly every mail server but surprisingly while the .NET framework has an SMTP class which allows sending mail it doesn’t have a corresponding POP3 class to allow you to pick it up.

The solution is to create one.

Start a new Windows application and add to it a new class (Project, Add Class) called POP3. To open a POP3 mail box you need to supply three pieces of information – the URL of the server, the user name and the password. It seems reasonable to open the mail box when the POP3 class is created. The constructor starts with:

public POP3(
string Server,
string user,
string password)
{
try
{

The first question is what is “try” all about? It is to do with error handling which in most projects we ignore because it’s not very interesting and would add considerably to the number of lines of code. In this case we can’t ignore error handling completely because so many things can go wrong in trying to connect to a POP3 server. It’s inevitable that errors will occur and will have to be dealt with.

To make the program more robust we can enclose any code we think might generate a runtime error in:

try
{
what you want to do

}
catch
{
what you do if it goes wrong

}

The try-catch construct is available in most modern  languages and is part of the philosophy of how errors should be handled by “exceptions”.

In this case all we are really doing is using the “try” clause to see if some code will work and it if doesn’t, we “catch” the error and replace it with something that will work.

What do we do next? The answer is that we need to set up a TCP/IP connection to the server and this is easy using the framework TcpClient class:

TCP = new TcpClient();
TCP.Connect(Server, 110);

The connection is made to the URL given in “Server” and to port 110 which is the default port for POP3 interactions.

The TcpClient class provides a Stream object which can be used to read and write byte data. It turns out to be slightly easier to hook up a StreamReader to the byte stream so that we can read complete lines of text:

POP3Stream = TCP.GetStream();
InStream = new StreamReader(POP3Stream);

You can think of streams as if they really were streams of data and in this case the stream of raw bytes provided by the TcpClient is “piped” into the StreamReader which converts it into easy to stream of character data.

Now we can communicate with the server, assuming it’s working and on-line. In the POP3 protocol the server sends us a message immediately the connection is made starting with “+OK”. If there is an error the lines starts with “-ERR”. So we need to wait for this to be received:

if (!WaitFor("+OK")) POPerror();

If we don’t get a “+OK” then we call an error reporting function and of course we have to remember to write both the POPerror and the WaitFor functions.

As soon as we have received the “+OK” the conversation continues with the client sending “USER username”, waiting for “+OK” again, and then sending “PASS password” and again waiting for “+OK”:

 SendData("USER " + user);
if (!WaitFor("+OK")) POPerror();
SendData("PASS " + password);
if (!WaitFor("+OK")) POPerror();
}

Of course we also have to write the SendData function.

At this point the connection is made and we have successfully logged into the POP3 server.

The final curly bracket closes the “try” clause and if it has all worked the constructor is complete. If any errors do occur in the “try” clause then it’s abandoned and the “catch” clause is started. What do we want to do if there is an error?

In this case nothing at all and so the catch clause is just:

 catch
{
}
}

So when the constructor returns we might or might not have an object with a connection to a working POP3 mailbox.

If you are going to develop this application further you probably need to add some code to report the error and handle it by attempting to correct it. For example, try again to connect.

To make all this work we also need to add:

using System.Net.Sockets;
using System.IO;

to the start of the class and the very minimal error reporting function:

void POPerror()
{
System.Windows.Forms.
MessageBox.Show("POP3 Error");
}

The WaitFor and Senddata functions are fairly straightforward:

Boolean WaitFor(String target)
{
do
{
data = InStream.ReadLine();
} while (data.Length == 0);
return data.StartsWith(target);
}

void SendData(string command)
{
byte[] outbuff = ASCIIEncoding.ASCII.
GetBytes(command + CRLF);
POP3Stream.Write(outbuff, 0,
command.Length + 2);
}

The WaitFor function can use the ReadLine method to read lines ending in carriage return linefeed, which is the unit of data the server deals in, and we simply loop until some data arrives. There is a timeout property that will generate an exception if the server takes too long and trigger the catch clause in the constructor.

The SendData function could use a StreamWriter but ensuring that it sent the correctly coded bytes is difficult. In this case it is much more direct to use the ASCIIEncoding object and its associated ASCII.GetBytes method to convert the Unicode string into an ASCII coded byte array which is then sent to the server. Notice the need to add a carriage return linefeed to the end of each command.

<ASIN:B000GRUNEU>

<ASIN:032140940X>

<ASIN:159257551X>



Last Updated ( Wednesday, 18 November 2015 )