|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe .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:
BackgroundInteracting with a POP3 ServerDownloading 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 '
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 & TracingQuite 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 To further help with the investigation of communication problems, a Using the codeServer SettingsI feel the server settings like IP address, etc. should not change within a session. ThePop3MailClient 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 Reading Raw EmailThe method AutoReconnect after server timeoutI tested the code extensively with Gmail, which sometimes simply fails to respond. If the Points of InterestEfficiencyMy 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 GmailI had a good experience using Gmail, although I encountered 2 flaws:
History
|
||||||||||||||||||||||