Using C# to Retrieve List of Emails via POP3

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:
<table><tr><td>
POP3 Server: <asp:TextBox ID="fld_Server" runat="server" /></td><td>Port: <asp:TextBox ID="fld_Port" runat="server" Text="110" Columns="3" /></td></tr>
<tr><td>Username: <asp:TextBox ID="fld_Username" runat="server" /></td><td>Password: <asp:TextBox ID="fld_Password" runat="server" TextMode="Password" />
</td></tr></table>
<asp:Button ID="btn_Connect" runat="server" Text="Connect" OnClick="btn_Connect_OnClick" /><br />
<asp:Literal ID="lit_Status" runat="server" />
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
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:
using System.Net.Sockets;
..
protected void btn_Connect_OnClick(object sender, EventArgs e)
{

}
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
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:
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(fld_Server.Text, Convert.ToInt32(fld_Port.Text));

NetworkStream netStream = tcpClient.GetStream();
System.IO.StreamReader strReader = new System.IO.StreamReader(netStream);

lit_Status.Text = strReader.ReadLine() + "<br />";
We used over 10 web hosting companies before we found Server Intellect. Their dedicated serversand add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
The next thing we will do is send our Username and Password to the server, and output the Server Response:
byte[] WriteBuffer = new byte[1024];
ASCIIEncoding enc = new System.Text.ASCIIEncoding();

WriteBuffer = enc.GetBytes("USER " + fld_Username.Text + "\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
lit_Status.Text += strReader.ReadLine() + "<br />";

WriteBuffer = enc.GetBytes("PASS " + fld_Password.Text + "\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
lit_Status.Text += strReader.ReadLine() + "<br />";
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:
WriteBuffer = enc.GetBytes("LIST\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);

String ListMessage;
while (true)
{
ListMessage = strReader.ReadLine();
if (ListMessage == ".")
{
break;
}
else
{
lit_Status.Text += ListMessage + "<br />";
continue;
}
}
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
Finally, to close the connection to the server, we send the command QUIT, and again, output the server response:
WriteBuffer = enc.GetBytes("QUIT\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
lit_Status.Text += strReader.ReadLine() + "<br />";
The full code-behind for this examples looks something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using System.Net.Sockets;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btn_Connect_OnClick(object sender, EventArgs e)
{
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(fld_Server.Text, Convert.ToInt32(fld_Port.Text));

NetworkStream netStream = tcpClient.GetStream();
System.IO.StreamReader strReader = new System.IO.StreamReader(netStream);

lit_Status.Text = strReader.ReadLine() + "<br />";

byte[] WriteBuffer = new byte[1024];
ASCIIEncoding enc = new System.Text.ASCIIEncoding();

WriteBuffer = enc.GetBytes("USER " + fld_Username.Text + "\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
lit_Status.Text += strReader.ReadLine() + "<br />";

WriteBuffer = enc.GetBytes("PASS " + fld_Password.Text + "\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
lit_Status.Text += strReader.ReadLine() + "<br />";

WriteBuffer = enc.GetBytes("LIST\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);

String ListMessage;
while (true)
{
ListMessage = strReader.ReadLine();
if (ListMessage == ".")
{
break;
}
else
{
lit_Status.Text += ListMessage + "<br />";
continue;
}
}

WriteBuffer = enc.GetBytes("QUIT\r\n");
netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
lit_Status.Text += strReader.ReadLine() + "<br />";
}
}

Comments