Skip to main content
Email Password   helpLost your password?

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

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:

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralAbout downloading attached file to mail Pin
friendprach
20:45 8 Oct '09  
GeneralPlease I need your Help Pin
vampraider12
15:25 6 Feb '09  
QuestionUnable to log on Pin
Member 3108417
9:06 13 Dec '08  
AnswerRe: Unable to log on Pin
Chad Z. Hower aka Kudzu
14:00 13 Dec '08  
QuestionNO Connection Could Be Made Because the Target Machine Actively Refused It Pin
oerslaafroze
6:37 27 Nov '07  
GeneralMIME Format Pin
Alan (Engeman)
1:50 21 Sep '07  
QuestionAmbiguous reference Pin
theplayer01
12:33 23 Aug '07  
AnswerRe: Ambiguous reference Pin
Chad Z. Hower aka Kudzu
11:23 26 Aug '07  
QuestionNeed Help Pin
kanzz
20:48 2 Aug '07  
GeneralSample for actual release of Indy.Sockets Pin
tim.taylor
3:51 16 Jul '07  
GeneralMessage body is blank Pin
Paolo Ramos
7:18 29 Nov '06  
GeneralRe: Message body is blank Pin
Chad Z. Hower aka Kudzu
8:52 29 Nov '06  
GeneralRe: Message body is blank Pin
Member 3905363
6:45 29 Nov '08  
QuestionMessage Dates Pin
ecs.dp
5:50 29 Aug '06  
AnswerRe: Message Dates Pin
ghiutzu
3:37 25 Aug '08  
GeneralSSL Pin
AMGG
0:13 7 Aug '06  
GeneralRe: SSL Pin
Chad Z. Hower aka Kudzu
0:45 8 Aug '06  
GeneralRe: SSL Pin
Niladri Mahapatra
21:43 9 Apr '07  
GeneralFirst Message always empty Pin
Haggy
4:00 6 Jul '06  
GeneralRe: First Message always empty Pin
Chad Z. Hower aka Kudzu
8:40 6 Jul '06  
GeneralMIME & VS2005 Pin
Haggy
3:49 6 Jul '06  
QuestionNot working with Yahoo emails Pin
boodi_81
2:24 19 May '06  
GeneralHow to retrieve only unread messages Pin
newbie13
7:56 18 Apr '06  
GeneralRe: How to retrieve only unread messages Pin
Chad Z. Hower aka Kudzu
9:24 20 Apr '06  
Questionsave Attachment Pin
r7809
5:41 20 Feb '06  


Last Updated 2 Jul 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009