Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / C#
Article

Retrieving Mail with POP3

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

Introduction

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

C#
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 = "127.0.0.1";
    string xUsername = "chad";
    string xPassword = "pass"; 
    using (POP3 xPOP3 = new POP3()) {
        xPOP3.Username = xUsername;
        xPOP3.Password = xPassword;
        xPOP3.Connect(xHost); 
        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);
            Console.WriteLine();
            Console.WriteLine(xMsg.Body.Text);
        }
        try {
        } 
        finally {
            xPOP3.Disconnect();
        }
    }
} 
catch (Exception e) {
    Console.WriteLine(e.Message);
}
Console.WriteLine("");
Console.WriteLine("Press enter");
Console.ReadLine();

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:

C#
string xHost = "127.0.0.1";
string xUsername = "chad";
string xPassword = "pass";

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

Output

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

Subject: Hello
From: chad@test.com
To: chad@local.net

Hello, this is a test message.

Press enter.

License

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


Written By
Cyprus Cyprus
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

 
QuestionHow to retrieve only unread messages Pin
newbie1318-Apr-06 6:56
newbie1318-Apr-06 6:56 
AnswerRe: How to retrieve only unread messages Pin
Chad Z. Hower aka Kudzu20-Apr-06 8:24
Chad Z. Hower aka Kudzu20-Apr-06 8:24 
Questionsave Attachment Pin
r780920-Feb-06 4:41
r780920-Feb-06 4:41 
AnswerRe: save Attachment Pin
newbie1310-Apr-06 11:24
newbie1310-Apr-06 11:24 
QuestionNon-ASCII characters? Pin
nj0y28-Jan-06 11:24
nj0y28-Jan-06 11:24 
QuestionRe: Non-ASCII characters? Pin
NoClone6-Sep-07 21:27
NoClone6-Sep-07 21:27 
Question[urgent]Removing messages from server Pin
bhatti8110-Nov-05 1:28
bhatti8110-Nov-05 1:28 
GeneralRe: [urgent]Removing messages from server Pin
Deza_19-Oct-06 6:46
Deza_19-Oct-06 6:46 
Have you a sample (code) for "until the POP3 session enters the UPDATE state -
succesfull QUIT command"
??

I use your Mail.dll and it does the work very well, but... I still have not can Delete really the emails in the server.

Thanks
Deza
GeneralRe: [urgent]Removing messages from server Pin
lesnikowski19-Oct-06 6:59
lesnikowski19-Oct-06 6:59 
GeneralAttachment Pin
niesp4-Oct-05 10:44
niesp4-Oct-05 10:44 
AnswerRe: Attachment Pin
dario.cravero12-Apr-06 17:36
dario.cravero12-Apr-06 17:36 
GeneralRe: Attachment Pin
idonen28-May-06 6:45
idonen28-May-06 6:45 
GeneralRe: Attachment Pin
SnotKnocker18-Aug-06 11:21
SnotKnocker18-Aug-06 11:21 
GeneralRe: Attachment Pin
Sonic.NET19-Sep-06 7:14
Sonic.NET19-Sep-06 7:14 
GeneralRe: Attachment Pin
Zorazorius16-Jan-07 0:29
Zorazorius16-Jan-07 0:29 
GeneralRe: Attachment Pin
Member 38640089-Feb-09 18:43
Member 38640089-Feb-09 18:43 
GeneralRe: Attachment Pin
luciano_lb4-Jul-07 5:32
luciano_lb4-Jul-07 5:32 
GeneralRe: Attachment Pin
klughing31-Aug-07 11:59
klughing31-Aug-07 11:59 
Questiondecoding subject and body Pin
Member 22155964-Sep-05 22:44
Member 22155964-Sep-05 22:44 
AnswerRe: decoding subject and body Pin
Chad Z. Hower aka Kudzu7-Sep-05 22:00
Chad Z. Hower aka Kudzu7-Sep-05 22:00 
QuestionRe: decoding subject and body Pin
Sonic.NET19-Sep-06 6:42
Sonic.NET19-Sep-06 6:42 
QuestionRe: decoding subject and body Pin
gostarme28-Sep-06 20:48
gostarme28-Sep-06 20:48 
Questionwhy not pure c#? Pin
Huisheng Chen2-Jul-05 19:05
Huisheng Chen2-Jul-05 19:05 
AnswerRe: why not pure c#? Pin
Chad Z. Hower aka Kudzu3-Jul-05 0:12
Chad Z. Hower aka Kudzu3-Jul-05 0:12 
GeneralRe: why not pure c#? Pin
Huisheng Chen3-Jul-05 5:36
Huisheng Chen3-Jul-05 5:36 

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.