Click here to Skip to main content
15,608,553 members
Articles / Programming Languages / C#
Posted 31 May 2006


294 bookmarked

POP3 Email Client (.NET 2.0)

Rate me:
Please Sign up or sign in to vote.
4.87/5 (100 votes)
4 Aug 2006CPOL5 min read
C# class making it easy to interact with POP3 email server, like listing of emails, download and deletion. Special attention was given to reliable connection control. The code was tested for several months with Google Gmail, including SSL.


The .NET framework 2.0 has revamped the support of email sending with improved SMTP classes, but receiving emails is still missing. There are various articles on CodeProject for POP3 support, but all have some drawbacks such as

  • some code is not managed
  • use of DLLs without .NET source code
  • too limited functionality and error reporting
  • no tracing of server commands and responses
  • no support for SSL
  • no XML documentation, etc.

This project builds on the previous projects, but is written entirely in C# 2.0. The present first article focuses on the downloading of raw emails from a POP3 server (RFC1939). There are methods to connect to a POP3 server, to get a list of available emails, to request some emails, to delete some emails and to disconnect. For debugging and for professional use, extensive error reporting and communication tracing is provided. A future article will decompose the received raw email into body, alternative views and attachments following the MIME specification.

I used Gmail for testing, which is freely available for anyone (recommended).

This code is based on the following work:


Interacting with a POP3 Server

Downloading an email from a POP3 server is rather straight forward. The communication with a POP3 server uses only few commands and is easily human readable. Once a connection, possibly with SSL, is established, the client needs to provide a user name and password to enter the POP3 state TRANSACTION, called 'connected' in Pop3MailClient.

In the connected (POP3: transaction) state, the client can execute the following commands:

  • STAT: GetMailboxStats(), number of mails and size of mailbox
  • LIST: GetEmailIdList(), a list with session message numbers and size for all emails
  • LIST [msg]: GetEmailSize(), session message number and size for just one email
  • UIDL: GetUniqueEmailIdList(), a list with unique ID numbers and session message numbers for all emails
  • NOOP: NOOP(), No operation, just check if server is still alive
  • RETR msg: GetRawEmail(), retrieve one complete email
  • DELE msg: DeleteEmail(), Mark one email for deletion once the connection is properly closed
  • RSET: UndeleteAllEmails(), POP3 server deletes unmarks the emails marked for deletion
  • QUIT: Disconnect(), POP3 server deletes emails as needed and closes TCP connection

For a better understanding, it is recommended to read the official POP3 specification, RFC1939 from IETF: Post Office Protocol - Version 3.

Error Handling & Tracing

Quite a number of things can go wrong when two computers communicate over the Internet. Therefore, solid error reporting and communication tracing is essential. Some problems, like no response form the server are fatal and throw an exception. After an exception, usually the connection is dead and needs to be rebuilt. If the error is detected by the POP3 client code, a Pop3Exception (inherited from ApplicationException) is thrown, otherwise it is a normal .NET exception. Some problems, like trying to retrieve a non existing email, raise just a Warning event. It is up to the user of the POP3 client code to decide if an exception should be thrown in the Warning event or a warning written into a log file or ... After a warning, the POP3 server is ready for the next command.

To further help with the investigation of communication problems, a Trace event is raised. It shows commands and responses exchanged between PopClient and PopServer, including warnings. It is strongly recommended to use this feature in the beginning of a project, because RFC1939 gives the server implementor great freedom. It often provides additional information which can be seen in the trace.

Using the code

Server Settings

I feel the server settings like IP address, etc. should not change within a session. The Pop3MailClient requires servername, port, should SSL be used, username and password in the constructor and they cannot be changed. If you want to connect to a different server or for a different user, create a new Pop3MailClient.

To get the demo code running, you need to enter your own credentials for username and password in the following line:

// TODO: Replace username and password with your own credentials.
Pop3.Pop3MailClient DemoClient =
  new Pop3.Pop3MailClient(
    "", 995, true, "", "password");

If you don't use Gmail, of course you need to also change the servername and port number, maybe even set useSSL to false.

Reading Raw Email

The method GetRawEmail returns the complete email content for one particular message number. RFC1939 specifies that only ANSI characters can be used and therefore the raw email can be easily displayed. But of course it might look funny because of special characters or encoding. Decoding an email will be part of the next article about my Pop3MimeClient class.

AutoReconnect after server timeout

I tested the code extensively with Gmail, which sometimes simply fails to respond. If the isAutoReconnect property is set, the Pop3MailClient tries to reconnect exactly once after a timeout. That's all it usually takes, but notice that any emails marked for deletion are not deleted on the server.

Points of Interest


My guess is that the garbage collector spends a considerable amount of time with collecting memory. Receiving email is a lot of text processing and the idea of all these strings created and discarded gives me a creepy feeling. I hear you saying, use Stringbuilders, but they can be even slower than strings if only few operations are executed with them. Isn't it time for some recycling, i.e. reusing the same global StringBuilder for every email received? I was careful not to introduce any concurrency problems. But even the Framework itself is not reentrant ! If the repeated use of StringBuffers troubles you, just make them local.


I had a good experience using Gmail, although I encountered 2 flaws:

  • Gmail shows maximal 250-260 mails in a mailbox, even if there are thousands. Gmail shows the oldest emails. As soon some emails get deleted (after disconnect), newer emails get available.
  • The response of Gmail is sometimes slow and sometimes there is just no response.


  • Update 5.August.2006: Fixed some bugs because autoreconnect didn't work properly
  • Update 1.July.2006: The code now supports connections with or without SSL. The UseSSL flag in the Pop3MailClient constructor indicates if the connection is with SSL or without
  • Version 1.0: One POP3 command I didn't need and therefore didn't implement was UIDL [id], which retrieves the message number for an unique ID


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
Singapore Singapore
Retired SW Developer from Switzerland living in Singapore

Interested in WPF projects.

Comments and Discussions

QuestionDownloading the attachment? Pin
Member 36510236-Mar-07 4:04
Member 36510236-Mar-07 4:04 
AnswerRe: Downloading the attachment? Pin
cwilliam19-Mar-07 10:25
cwilliam19-Mar-07 10:25 
QuestionRe: Downloading the attachment? Pin
kiquenet.com28-Mar-07 23:20
professionalkiquenet.com28-Mar-07 23:20 
AnswerRe: Downloading the attachment? Pin
cwilliam29-Mar-07 7:12
cwilliam29-Mar-07 7:12 
QuestionSSL Authentication and certificat Pin
OnurCel5-Mar-07 21:01
OnurCel5-Mar-07 21:01 
GeneralNot able to delete the message Pin
Nitin198127-Feb-07 19:31
Nitin198127-Feb-07 19:31 
GeneralFails to connect Pin
victorbos9-Feb-07 15:33
victorbos9-Feb-07 15:33 
GeneralRe: Fails to connect Pin
victorbos12-Feb-07 4:49
victorbos12-Feb-07 4:49 
It turns out that on my network, PORT 995 has been blocked, which is why this didn't work as is. A friend tried the program from another network (where the PORT wasn't blocked) and it worked nicely. Thanks for the xample.Smile | :)
GeneralGreat Program has really helped me Pin
erikkl20005-Feb-07 19:29
erikkl20005-Feb-07 19:29 
QuestionGet always TextPlain Message [modified] Pin
BrcH793-Feb-07 6:25
BrcH793-Feb-07 6:25 
AnswerRe: Get always TextPlain Message Pin
MSGUY19-Jul-07 2:16
MSGUY19-Jul-07 2:16 
GeneralRe: Get always TextPlain Message Pin
BrcH7919-Aug-07 15:38
BrcH7919-Aug-07 15:38 
GeneralsendMessage problem Pin
Mad200629-Jan-07 7:21
Mad200629-Jan-07 7:21 
Generaldownload the message as eml file Pin
BMWABCD18-Jan-07 12:31
BMWABCD18-Jan-07 12:31 
GeneralRe: download the message as eml file Pin
cwilliam19-Mar-07 10:27
cwilliam19-Mar-07 10:27 
QuestionHow can i read emails in the reverse direction... Pin
Mahernoz11-Jan-07 20:19
Mahernoz11-Jan-07 20:19 
AnswerRe: How can i read emails in the reverse direction... Pin
Peter Huber SG12-Jan-07 1:15
mvaPeter Huber SG12-Jan-07 1:15 
GeneralRe: How can i read emails in the reverse direction... Pin
Mahernoz14-Jan-07 17:09
Mahernoz14-Jan-07 17:09 
GeneralRe: How can i read emails in the reverse direction... Pin
Peter Huber SG14-Jan-07 20:19
mvaPeter Huber SG14-Jan-07 20:19 
GeneralRetrieving only unread mail Pin
Josh19911826-Nov-06 22:52
Josh19911826-Nov-06 22:52 
AnswerRe: Retrieving only unread mail Pin
Peter Huber SG27-Nov-06 4:27
mvaPeter Huber SG27-Nov-06 4:27 
QuestionRe: Retrieving only unread mail Pin
meshobhon27-Nov-06 14:25
meshobhon27-Nov-06 14:25 
AnswerRe: Retrieving only unread mail Pin
Peter Huber SG27-Nov-06 14:38
mvaPeter Huber SG27-Nov-06 14:38 
GeneralRe: Retrieving only unread mail Pin
Josh1991181-Dec-06 17:56
Josh1991181-Dec-06 17:56 
GeneralRe: Retrieving only unread mail Pin
Peter Huber SG1-Dec-06 23:30
mvaPeter Huber SG1-Dec-06 23:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.