Click here to Skip to main content
13,194,662 members (87,729 online)
Click here to Skip to main content
Add your own
alternative version


293 bookmarked
Posted 31 May 2006

POP3 Email Client (.NET 2.0)

, 4 Aug 2006
Rate this:
Please Sign up or sign in to vote.
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)


About the Author

Peter Huber SG
Software Developer (Senior)
Switzerland Switzerland
SW Developer from Switzerland

I would be very interested to do SW development for an international WPF project.

You may also be interested in...

Comments and Discussions

QuestionMy inbox. Pin
iProgramIt25-Jul-15 15:18
professionaliProgramIt25-Jul-15 15:18 
QuestionI connect but I see no emails Pin
thiago martins do carmo5-Apr-13 8:41
memberthiago martins do carmo5-Apr-13 8:41 
QuestionWhere I get Email IDs Pin
Muhammad Mohiuddin Bukhari26-Feb-13 21:06
memberMuhammad Mohiuddin Bukhari26-Feb-13 21:06 
Questionretrive email by pop3 in .net2 Pin
Member 969113021-Dec-12 19:54
memberMember 969113021-Dec-12 19:54 
Questioncannot connect to exchange server Pin
Member 910346511-Dec-12 23:56
memberMember 910346511-Dec-12 23:56 
QuestionI connect but I see no emails Pin
muslera4-Nov-12 12:45
membermuslera4-Nov-12 12:45 
QuestionConverting from plan to mail Pin
Eduardo Mass12-Oct-12 6:27
memberEduardo Mass12-Oct-12 6:27 
BugGetEmailIdList() always returns 0 records Pin
suhail wani13-Sep-12 8:05
membersuhail wani13-Sep-12 8:05 
Questionhow can i create my own mailing system using 3.5 Pin
KhanFaiz7-Sep-12 20:24
memberKhanFaiz7-Sep-12 20:24 
I developed a school software where i want that the student interact with each other with email within the software.
1) how can i develop the mailing system?
2) the mail is stored in the database(MSsql etc) or simple stored as a text file?
3) how the process goes?
etc etc...
Thanks in anticipation

QuestionThanks for the post, how can i get the mail subjects? Pin
modemode5-Jun-12 12:07
membermodemode5-Jun-12 12:07 
Questionhow to fetch the attachment file in the mail Pin
klita131-May-12 10:55
memberklita131-May-12 10:55 
QuestionHow to save attachment to local harddisk? Pin
Wilson Chow8-May-12 18:06
memberWilson Chow8-May-12 18:06 
QuestionA connection attempt failed Pin
Member 811792327-Apr-12 6:30
memberMember 811792327-Apr-12 6:30 
Questionhow can reduce ReadTimeout of Pop3MimeClient Pin
rpc.ragesh14-Mar-12 2:22
memberrpc.ragesh14-Mar-12 2:22 
AnswerRe: how can reduce ReadTimeout of Pop3MimeClient Pin
rpc.ragesh15-Mar-12 2:22
memberrpc.ragesh15-Mar-12 2:22 
GeneralMy vote of 5 Pin
Md Kamruzzaman Sarker25-Nov-11 20:17
memberMd Kamruzzaman Sarker25-Nov-11 20:17 
QuestionNo connection could be made because the target machine actively refused it Pin
Ramajayam198919-Nov-11 2:45
memberRamajayam198919-Nov-11 2:45 
QuestionPop3MimeClient Pin
Sumit Kumar Singh India24-Oct-11 2:40
memberSumit Kumar Singh India24-Oct-11 2:40 
GeneralRead Email Special chars Pin
elliot_pineda3-May-11 14:45
memberelliot_pineda3-May-11 14:45 
GeneralMy vote of 5 Pin
sampsonlau4-Apr-11 18:33
membersampsonlau4-Apr-11 18:33 
Generalproblem with msg uid > returning size Pin
eidylon11-Mar-11 9:57
membereidylon11-Mar-11 9:57 
GeneralRe: problem with msg uid > returning size Pin
eidylon11-Mar-11 10:20
membereidylon11-Mar-11 10:20 
GeneralMy vote of 5 Pin
Ashish Tyagi 4021-Feb-11 6:47
memberAshish Tyagi 4021-Feb-11 6:47 
GeneralMy vote of 5 Pin
burleyman248-Feb-11 9:28
memberburleyman248-Feb-11 9:28 
GeneralMy vote of 5 Pin
mhn21724-Sep-10 23:57
membermhn21724-Sep-10 23:57 
GeneralMy vote of 5 Pin
hamid.khan13524-Sep-10 2:35
memberhamid.khan13524-Sep-10 2:35 
GeneralMy vote of 3 Pin
Thien La Duc13-Sep-10 16:42
memberThien La Duc13-Sep-10 16:42 
GeneralRe: My vote of 3 Pin
Nishant Sivakumar2-Oct-10 10:12
mvpNishant Sivakumar2-Oct-10 10:12 
QuestionGMAIL??? Pin
hesaigo999ca3-Aug-10 6:48
memberhesaigo999ca3-Aug-10 6:48 
AnswerRe: GMAIL??? Pin
Gregoryagu4-Aug-10 14:24
memberGregoryagu4-Aug-10 14:24 
GeneralRe: GMAIL??? Pin
hesaigo999ca6-Aug-10 7:38
memberhesaigo999ca6-Aug-10 7:38 
GeneralRe: GMAIL??? Pin
hesaigo999ca7-Aug-10 9:57
memberhesaigo999ca7-Aug-10 9:57 
QuestionHow to Read folder lik "junk" or any customized folder on mailserver? Pin Pin
jymitra19-Jul-10 21:45
memberjymitra19-Jul-10 21:45 
GeneralI am looking into version; Do any body have that? Please Would be greate help Pin
r_ozone4-Jun-10 3:57
memberr_ozone4-Jun-10 3:57 
GeneralRe: I am looking into version; Do any body have that? Please Would be greate help Pin
WinXpProme8-Jun-10 11:46
memberWinXpProme8-Jun-10 11:46 
GeneralRe: I am looking into version; Do any body have that? Please Would be greate help Pin
rpc.ragesh15-Mar-12 2:23
memberrpc.ragesh15-Mar-12 2:23 
GeneralDetermine original sender Pin
mick12325-May-10 6:51
membermick12325-May-10 6:51 
GeneralReading email seems to be failing [modified] Pin
Caden17-Dec-09 6:55
memberCaden17-Dec-09 6:55 
QuestionHow to decode the subject? Pin
Mikla7825-Nov-09 5:12
memberMikla7825-Nov-09 5:12 
AnswerRe: How to decode the subject? Pin
Peter Huber SG25-Nov-09 13:18
memberPeter Huber SG25-Nov-09 13:18 
GeneralMark message as read Pin
thomasabcd21-Sep-09 9:20
memberthomasabcd21-Sep-09 9:20 
GeneralRe: Mark message as read Pin
tokfrans5-Oct-09 5:21
membertokfrans5-Oct-09 5:21 
Generalsmall real world(?) patch Pin
tb200017-Sep-09 4:19
membertb200017-Sep-09 4:19 
QuestionHow to Track Bounce Back Email Pin
gowrikaran198414-Aug-09 3:00
membergowrikaran198414-Aug-09 3:00 
AnswerRe: How to Track Bounce Back Email Pin
Peter Huber SG14-Aug-09 15:31
memberPeter Huber SG14-Aug-09 15:31 
Generali'm attempting a re-write Pin
Yankee Imperialist Dog!13-Jul-09 9:43
memberYankee Imperialist Dog!13-Jul-09 9:43 
GeneralAttachments. Pin
Igor11111116-May-09 21:48
memberIgor11111116-May-09 21:48 
GeneralRe: Attachments. Pin
tb200017-Sep-09 4:22
membertb200017-Sep-09 4:22 
Questionhow to get count of unread email? Pin
chjlcn11-Oct-08 18:40
memberchjlcn11-Oct-08 18:40 
Generalgmail usage, is there a way Pin
hesaigo999ca8-Oct-08 9:19
memberhesaigo999ca8-Oct-08 9:19 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171018.2 | Last Updated 5 Aug 2006
Article Copyright 2006 by Peter Huber SG
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid