Library
tutorials & articles
How to POP3 in C#
Introduction
This is the second in a series of articles on Internet programming with Microsoft's new C# programming language. Inthe first article, I wrote a simple SMTP class. In this article, I'm going to write a simple POP3 class. The SMTP classthat I wrote was not very useful, except maybe as an exercise, as there already exists a similar SMTP class in the
Web.Mail
namespace of the .NET framework called
SmtpMail
. Our POP3 class in this article will be a little moreuseful as it doesn't already exist in the .NET framework. I have encountered many POP3 C# classes in my searchesof the Internet and most were sufficient to begin programming email clients.
Getting Started
I usually begin writing new classes by introducing an exception class that I can use to throw and catch all exceptionsof the class.I will not explain the exception class, but rather I expect the reader have enough expertise with C# to understand thisexception class before reading the rest of the article.Next I created a small class that defines a POP3 message.When you retrieve lists of POP3 messages from a POP3 server, the list includes a message number and number ofbytes. You can then use the message number to retrieve the message content. You'll see this later when we defineour List and Retrieve methods. We derive our Pop3 class from the
System. Net.Sockets.TcpClient
class inthe .NET framework.The
TcpClient
class and the other classes in the
System.Net.Sockets
namespace of the .NET framework aregreat encapsulations of the familiar function-oriented socket library.
Connecting and Disconnecting
The first method of our Pop3 class is the Connect method. This method takes a server name, username andpassword parameter to connect to a remote (sometimes local) POP3 server.We begin by calling the
TcpClient.Connect
method passing the server name and the 110 port. The 110 portnumber is the well known port number for POP3 operations. What that means is that POP3 servers by default shouldlisten for connections on port 110. When the POP3 server connects to a client, it should immediately respond with the
+OK
acknowledgement message. Next we send two messages,
USER
and
PASS
, back to the server. The POP3 servershould acknowledge a successful login by acknowledging both messages. If the POP3 server returns anything but
public class Pop3Exception : System. ApplicationException{ public Pop3Exception( string str) : base( str) { }}public class Pop3Message{ public long number; publiclong bytes;
public bool retrieved; public string message;}public class Pop3 : System.Net.Sockets.TcpClient {public void Connect(string server, string username, string password){ string message; string response; Connect(server, 110); response = Response(); if (response.Substring(0, 3) != "+OK") { throw new Pop3Exception(response); } message = "USER " + username + "\r\n"; Write(message); response = Response(); if (response.Substring(0, 3) != "+OK") { throw new Pop3Exception(response); } message = "PASS " + password + "\r\n"; Write(message); response = Response(); if (response.Substring(0, 3) != "+OK") { throw new Pop3Exception(response); }}
By
Randy Charles Morin
, published on 01 Oct 2003
Comments (5)
PDF
Join us Sign in Chelmsford
Want to stay in touch with what's going on? Follow us on twitter
!
Leave a Comment