Click here to Skip to main content
11,649,678 members (78,364 online)
Click here to Skip to main content

Retrieving Mail with POP3

, 2 Jul 2005 CPOL 167K 4.2K 41
Rate this:
Please Sign up or sign in to vote.
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

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.

License

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

Share

About the Author

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

Formerly the Regional Developer Adviser (DPE) for Microsoft Middle East and Africa, he was responsible for 85 countries spanning 4 continents and 10 time zones. Now Chad is a Microsoft MVP.

Chad is the chair of several popular open source projects including Indy and Cosmos (C# Open Source Managed Operating System).

Chad is the author of the book Indy in Depth and has contributed to several other books on network communications and general programming.

Chad has lived in Canada, Cyprus, Switzerland, France, Jordan, Russia, Turkey, and the United States. Chad has visited more than 60 countries, visiting most of them several times.

You may also be interested in...

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 
There are several forms of auth. Its possible your server does not accept basic auth. You need to find out what type it supports and configure the client to use it.

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

My Technical Stuff:
http://www.KudzuWorld.com

My Blogs:
http://www.KudzuWorld.com/blogs/

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 
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 
GeneralRe: SSL Pin
algoram7-Jul-14 5:32
memberalgoram7-Jul-14 5:32 
GeneralFirst Message always empty Pin
Haggy6-Jul-06 3:00
memberHaggy6-Jul-06 3:00 
GeneralRe: First Message always empty Pin
Chad Z. Hower aka Kudzu6-Jul-06 7:40
memberChad Z. Hower aka Kudzu6-Jul-06 7:40 
GeneralMIME & VS2005 Pin
Haggy6-Jul-06 2:49
memberHaggy6-Jul-06 2:49 
QuestionNot working with Yahoo emails Pin
boodi_8119-May-06 1:24
memberboodi_8119-May-06 1:24 
QuestionHow to retrieve only unread messages Pin
newbie1318-Apr-06 6:56
membernewbie1318-Apr-06 6:56 
AnswerRe: How to retrieve only unread messages Pin
Chad Z. Hower aka Kudzu20-Apr-06 8:24
memberChad Z. Hower aka Kudzu20-Apr-06 8:24 
Questionsave Attachment Pin
r780920-Feb-06 4:41
memberr780920-Feb-06 4:41 
AnswerRe: save Attachment Pin
newbie1310-Apr-06 11:24
membernewbie1310-Apr-06 11:24 
QuestionNon-ASCII characters? Pin
nj0y28-Jan-06 11:24
membernj0y28-Jan-06 11:24 
QuestionRe: Non-ASCII characters? Pin
NoClone6-Sep-07 21:27
memberNoClone6-Sep-07 21:27 
Question[urgent]Removing messages from server Pin
bhatti8110-Nov-05 1:28
memberbhatti8110-Nov-05 1:28 
AnswerRe: [urgent]Removing messages from server Pin
lesnikowski21-Dec-05 3:55
memberlesnikowski21-Dec-05 3:55 
GeneralRe: [urgent]Removing messages from server Pin
Rojano19-Oct-06 6:46
memberRojano19-Oct-06 6:46 
GeneralRe: [urgent]Removing messages from server Pin
lesnikowski19-Oct-06 6:59
memberlesnikowski19-Oct-06 6:59 
GeneralAttachment Pin
niesp4-Oct-05 10:44
memberniesp4-Oct-05 10:44 
AnswerRe: Attachment Pin
dario.cravero12-Apr-06 17:36
memberdario.cravero12-Apr-06 17:36 
GeneralRe: Attachment Pin
idonen28-May-06 6:45
memberidonen28-May-06 6:45 
GeneralRe: Attachment Pin
SnotKnocker18-Aug-06 11:21
memberSnotKnocker18-Aug-06 11:21 
GeneralRe: Attachment Pin
Sonic.NET19-Sep-06 7:14
memberSonic.NET19-Sep-06 7:14 
GeneralRe: Attachment Pin
Zorazorius16-Jan-07 0:29
memberZorazorius16-Jan-07 0:29 
GeneralRe: Attachment Pin
Member 38640089-Feb-09 18:43
memberMember 38640089-Feb-09 18:43 
GeneralRe: Attachment Pin
luciano_lb4-Jul-07 5:32
memberluciano_lb4-Jul-07 5:32 
GeneralRe: Attachment Pin
klughing31-Aug-07 11:59
memberklughing31-Aug-07 11:59 
Questiondecoding subject and body Pin
zangu4-Sep-05 22:44
memberzangu4-Sep-05 22:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150804.4 | Last Updated 2 Jul 2005
Article Copyright 2005 by Chad Z. Hower aka Kudzu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid