On this blog I regularly publish articles with tips and tricks for the programming language C# .Net.
C# is a modern, object-oriented programming language, which fully ultilizes the possibilites of the .Net framework. I also write about app programming for Android via C#, as well as PHP and Matlab. The difficulty of this blog is supposed to be variable, I hope for beginners and experts there is always something to look for.
If you have questions or suggestions, I am happy about your emails.

Tuesday, September 14, 2010

Receive Emails

Attention: This post describes the basic of receiving emails. At the time of this post this was possible without the use of SSL, nowadays this is required by most providers Therefore I explain the receiving of emails with an SSL connection in this new post. There also the usage of an IMAP server is described - in this post only a POP3 server is considered.

In a previous post I showed, how to send emails with C#.
Today is about receiving emails using C#.
Sending was relatively easy, since .Net provided methods to connect to an SMTP server, over whcih emails can be send.
Receiving is a bit harder, we have to logon to the incoming mail server and send the right commands to check and geet emails.
Clients can get emails from the mail server usign the POP3 or IMAP protocoll. IMAP provides more functionality, the most free mail providers like WEB or GMX though use POP3.
The code shown here also works with the POP3 protocoll.
A POP3 server waits for requests on a specific port (mostly 110). A client then can input username and password there and then send commands, which all have to be separated by a linebreak.
The server sends to every command and answer and waits for input, if necessary.
We impement the POP3 server in C# with a TcpClient and send the commands via a NetworkStream.
Before I go into more details of the code, first I want to explain the basic process of communication with the incoming mail server:
After setting up the connection this first expects a username, which we set with the command "USER username". Then the password follows with "PASS password".
To list all mails available on the server the command "LIST" is needed. In response to that, the server sends all messages as an answer - for each ID and size of the message, per line one message, the end of a line is characterized by ".".
One specific email can be retrieved via "RETR id". The server then answers with the complete content.
To terminate the connection to the server, there is the command "QUIT".

Now to the implementation:

To represent the POP3 server an instance of the class TcpClient. To send the commands described above, we use the class NetworkStream, which connects to the stream of the server.
Since computers work with binary number, but we humans understand words over our alphabet better, we have to convert the commands to bytecode, which can be done with the class ASCIIEncoding.
An instance of the class StreamReader eventually is used to read the answers sent by the server.
The following program demonstrates a communication with the incoming mail server, the programming style (using global variables etc.) is not the best, but I think, the basic principles should become clear quickly in this way.
The program consists of 4 functions, which are all called from Form1_Load():

// StringBuilder for a quicker loading of the mail content
StringBuilder FullEmailContent = new StringBuilder();

/* as long as not "." is read, the email is not at its end, write read lines in the StringBuilder */string TempLine = StreamListener.ReadLine();
while (TempLine != ".")
{
FullEmailContent.Append(TempLine + "\r\n");
TempLine = StreamListener.ReadLine();
}

/* as long as not "." is read, there are more emails available on the server, iterate through them and output ID + size */string CurrentMessage = StreamListener.ReadLine();
while (CurrentMessage != ".")
{
MessageBox.Show(CurrentMessage);
CurrentMessage = StreamListener.ReadLine();
}
}

}
}

The function ReadMailContent() writes the content of the read mail in a RichTextBox (which has to be available on the form), since this control supports basic formattings (like links and split lines), like they occur in emails.
When using the function Connect(), the parameters have to be replacecd by own ones.
For the free mail providers WEB and GMX the following parameters are needed: