Click here to Skip to main content
15,039,359 members
Articles / Programming Languages / C#
Posted 2 Jul 2005


41 bookmarked

Retrieving Mail with POP3

Rate me:
Please Sign up or sign in to vote.
3.04/5 (11 votes)
2 Jul 2005CPOL2 min read
In this article I will demonstrate how to quickly and easily retrieve mail messages using the POP3 protocol.


In this article, I will demonstrate how to quickly and easily retrieve mail messages using the POP3 protocol.

Why Console?

The demo code is a console application. For simple code snippets, I prefer console applications as they are easy to write and focus on the code without the demo being over shadowed by the interface code. All of the code demonstrated here can of course be used in Win Forms, Web Forms, Web service or any other type of applications.

What is Indy?

This demo uses classes from the open source library Indy.Sockets. Indy.Sockets is an open source socket library that supports clients, servers, TCP, UDP, raw sockets, as well as over 100 higher level protocols such as SMTP, POP3, NNTP, HTTP and many more. Indy.Sockets is available for C#, C++, Delphi, Visual Basic. NET, or any .NET language, and Kylix.

The code

try {
    // You will need to set these to your settings.
    // The settings here are settings for a local 
    // demo POP3 server
    // that I use for testing.
    string xHost = "";
    string xUsername = "chad";
    string xPassword = "pass"; 
    using (POP3 xPOP3 = new POP3()) {
        xPOP3.Username = xUsername;
        xPOP3.Password = xPassword;
        int xCount = xPOP3.CheckMessages();
        if (xCount == 0) {
            Console.WriteLine("No messages on account.");
        else {
            Message xMsg = new Message();
            xPOP3.Retrieve(1, xMsg);
            Console.WriteLine("Subject: " + xMsg.Subject);
            Console.WriteLine("From: " + xMsg.From.Text);
            Console.WriteLine("To: " + xMsg.Recipients[0].Text);
        try {
        finally {
catch (Exception e) {
Console.WriteLine("Press enter");

The code is very simple. The POP3 server is connected by using the host, username and the password provided. The CheckMessages method is called to obtain the number of messages in the mailbox. If at least one message exists, the first message is retrieved. Note the use of the Message class this is a special container that parses the mail message and its headers for easy access by the developer. Note the explicit use of the Disconnect method. In TCP protocols it's good to explicitly disconnect and with the POP3 protocol it is especially important as POP3 is transactional. Failing to call Disconnect will cause the POP3 server to rollback any messages deleted during the connection.

Running the demo

It is important to change these lines:

string xHost = "";
string xUsername = "chad";
string xPassword = "pass";

You need to fill in these with the settings for your POP3 server.


Running the demo will produce an output similar to this, if you have a message in your mailbox:

Subject: Hello

Hello, this is a test message.

Press enter.


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


About the Author

Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"

I am a former Microsoft Regional DPE (MEA) covering 85 countries, former Microsoft Regional Director, and 10 Year Microsoft MVP.

I have lived in Bulgaria, Canada, Cyprus, Switzerland, France, Jordan, Russia, Turkey, The Caribbean, and USA.

Creator of Indy, IntraWeb, COSMOS, X#, CrossTalk, and more.

Comments and Discussions

GeneralAbout downloading attached file to mail Pin
friendprach8-Oct-09 19:45
Memberfriendprach8-Oct-09 19:45 
GeneralPlease I need your Help Pin
vampraider126-Feb-09 14:25
Membervampraider126-Feb-09 14:25 
QuestionUnable to log on Pin
Member 310841713-Dec-08 8:06
MemberMember 310841713-Dec-08 8:06 
AnswerRe: Unable to log on Pin
Chad Z. Hower aka Kudzu13-Dec-08 13:00
MemberChad Z. Hower aka Kudzu13-Dec-08 13:00 
QuestionNO Connection Could Be Made Because the Target Machine Actively Refused It Pin
oerslaafroze27-Nov-07 5:37
Memberoerslaafroze27-Nov-07 5:37 
GeneralMIME Format Pin
Alan (Engeman)21-Sep-07 0:50
MemberAlan (Engeman)21-Sep-07 0:50 
QuestionAmbiguous reference Pin
theplayer0123-Aug-07 11:33
Membertheplayer0123-Aug-07 11:33 
AnswerRe: Ambiguous reference Pin
Chad Z. Hower aka Kudzu26-Aug-07 10:23
MemberChad Z. Hower aka Kudzu26-Aug-07 10:23 
QuestionNeed Help Pin
kanzz2-Aug-07 19:48
Memberkanzz2-Aug-07 19:48 
GeneralSample for actual release of Indy.Sockets Pin
tim.taylor16-Jul-07 2:51
Membertim.taylor16-Jul-07 2:51 
GeneralMessage body is blank Pin
Paolo Ramos29-Nov-06 6:18
MemberPaolo Ramos29-Nov-06 6:18 
GeneralRe: Message body is blank Pin
Chad Z. Hower aka Kudzu29-Nov-06 7:52
MemberChad Z. Hower aka Kudzu29-Nov-06 7:52 
GeneralRe: Message body is blank Pin
Member 390536329-Nov-08 5:45
MemberMember 390536329-Nov-08 5:45 
QuestionMessage Dates Pin
ecs.dp29-Aug-06 4:50
Memberecs.dp29-Aug-06 4:50 
AnswerRe: Message Dates Pin
ghiutzu25-Aug-08 2:37
Memberghiutzu25-Aug-08 2:37 
GeneralSSL Pin
AMGG6-Aug-06 23:13
MemberAMGG6-Aug-06 23:13 
GeneralRe: SSL Pin
Chad Z. Hower aka Kudzu7-Aug-06 23:45
MemberChad Z. Hower aka Kudzu7-Aug-06 23:45 
GeneralRe: SSL Pin
Niladri Mahapatra9-Apr-07 20:43
MemberNiladri Mahapatra9-Apr-07 20:43 
GeneralRe: SSL Pin
Daniiiii24-Sep-12 21:44
MemberDaniiiii24-Sep-12 21:44 
GeneralRe: SSL Pin
algoram7-Jul-14 0:09
Memberalgoram7-Jul-14 0:09 
GeneralRe: SSL Pin
Chad Z. Hower aka Kudzu7-Jul-14 4:17
MemberChad Z. Hower aka Kudzu7-Jul-14 4:17 
As the Delphi.NET compiler is now dead, so is this project unfortunately. This article is nearly 10 years old.
Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"

My Technical Stuff:

My Blogs:

GeneralRe: SSL Pin
algoram7-Jul-14 5:16
Memberalgoram7-Jul-14 5:16 
GeneralRe: SSL Pin
Chad Z. Hower aka Kudzu7-Jul-14 5:18
MemberChad Z. Hower aka Kudzu7-Jul-14 5:18 
GeneralRe: SSL Pin
algoram7-Jul-14 5:22
Memberalgoram7-Jul-14 5:22 
GeneralRe: SSL Pin
Chad Z. Hower aka Kudzu7-Jul-14 5:25
MemberChad Z. Hower aka Kudzu7-Jul-14 5:25 

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.