This tutorial will show you how to use ASP.NET and C# to download a list of emails using a POP3 server.

In this tutorial, we will give you an introduction into how to use ASP.NET to login to a POP3 server and check your email. We will be using the System.Net.Sockets namespace to utilize the TcpClient and NetworkStream classes for this example.

The first thing we want to do is create our ASPX page. We will keep it simple and add TextBoxes, a button and a Literal control to the page:

We are going to use the button to initiate the connection to the POP3 server. We give the user the ability to input their own server, and login credentials. Notice the method attached to the Click event of the button. We should have the following handler in the codebehind, along with the assembly reference:

We can use this handler for all of our code, seeing as we are just connecting to the server and checking messages for this example. In a real-world application, you may want to create a new class for the POP3 functionality.
We start by first create a new instance of the TcpClient, and attempt to connect to the POP3 server. We also instantiate a NetworkStream object, then we output the server response to our Literal control:

The next thing we will do is send our Username and Password to the server, and output the Server Response:

If the Username and Password are correct and verified by the Server, we will get a response starting +OK.
Now, to retrieve a list of messages from the server, we first request using the LIST command, and then we will want to loop through each message to output:

Finally, to close the connection to the server, we send the command QUIT, and again, output the server response:

The full code-behind for this examples looks something like this:

Download Source Files