Skip to main content
Email Password   helpLost your password?

Introduction

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

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:

Background

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:

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(
    "pop.gmail.com", 995, true, "Username@gmail.com", "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

Efficiency

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.

Gmail

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

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralMark message as read Pin
thomasabcd
10:20 21 Sep '09  
GeneralRe: Mark message as read Pin
tokfrans
6:21 5 Oct '09  
Generalsmall real world(?) patch Pin
tb2000
5:19 17 Sep '09  
GeneralHow to Track Bounce Back Email Pin
gowrikaran1984
4:00 14 Aug '09  
GeneralRe: How to Track Bounce Back Email Pin
Peter Huber SG
16:31 14 Aug '09  
Generali'm attempting a re-write Pin
Yankee Imperialist Dog!
10:43 13 Jul '09  
GeneralAttachments. Pin
Igor1111111
22:48 6 May '09  
GeneralRe: Attachments. Pin
tb2000
5:22 17 Sep '09  
Questionhow to get count of unread email? Pin
chjlcn
19:40 11 Oct '08  
Generalgmail usage, is there a way Pin
hesaigo999ca
10:19 8 Oct '08  
GeneralSuparb Ummmmmmh u are genius... and true Social Worker Pin
Member 4498912
20:21 25 Sep '08  
GeneralThank you! Pin
Tomi84
1:36 5 Sep '08  
QuestionRegarding downloading new mails Pin
Dashrath Wasmatkar
20:33 31 Aug '08  
GeneralWell Done Pin
Randy Charles Morin
5:54 3 Jul '08  
GeneralThank you! Pin
msdevdude
20:15 3 May '08  
GeneralThank you Pin
net_prog
12:53 12 Mar '08  
GeneralRe: Thank you Pin
Peter Huber SG
16:32 12 Mar '08  
GeneralHow to skip downloading an attachment? Pin
Raul Rupsingh
16:55 2 Feb '08  
AnswerRe: How to skip downloading an attachment? Pin
Peter Huber SG
5:03 4 Feb '08  
GeneralRe: How to skip downloading an attachment? Pin
hesaigo999ca
10:22 8 Oct '08  
GeneralRe: How to skip downloading an attachment? Pin
Shlomy
5:07 19 Mar '09  
GeneralSolution to error retrieving messages Pin
tbrewer
12:42 23 Jan '08  
GeneralRe: Solution to error retrieving messages Pin
Peter Huber SG
16:40 23 Jan '08  
GeneralTracked this down further... Pin
tbrewer
6:25 24 Jan '08  
GeneralRe: Tracked this down further... Pin
frankfpp
7:57 23 Oct '09  


Last Updated 4 Aug 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009