5,696,576 members and growing! (14,494 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Internet & Network     Intermediate License: The Code Project Open License (CPOL)

Retrieving Mail with POP3

By Chad Z. Hower aka Kudzu

In this article I will demonstrate how to quickly and easily retrieve mail messages using the POP3 protocol.
C#, Windows, .NET, Visual Studio, Dev

Posted: 2 Jul 2005
Updated: 2 Jul 2005
Views: 72,191
Bookmarked: 27 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 3.05 Rating: 3.05 out of 5
2 votes, 20.0%
1
0 votes, 0.0%
2
2 votes, 20.0%
3
1 vote, 10.0%
4
5 votes, 50.0%
5

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)

About the Author

Chad Z. Hower aka Kudzu


Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"
Website: http://www.KudzuWorld.com
Blogspace: http://www.KudzuWorld.com/blogs/
Speaking Profile: http://www.woo-hoo.net/

Formerly the Regional Developer Adviser (DPE) for Microsoft MEA (Middle East and Africa), he was responsible for 85 countries spanning 4 continents and crossing 10 time zones. Now Chad is Microsoft MVP and a Microsoft Regional Director covering Europe, Middle East, Africa, and Asia and a professional speaker at popular developer conferences worldwide. Chad was once introduced as having "mastered more languages than a United Nations translator." Chad is the author of the book Indy in Depth and has contributed to several other books on network communications and general programming. Chad writes regularly for the Software Developer Network Magazine (Dutch), and occasionally for other magazines. Chad is an expatriate who travels extensively year round. Chad has lived in Canada, Cyprus, Jordan, Russia, Turkey, and the United States. In total Chad has visited more than 50 countries, visiting most of them many times.
Occupation: Software Developer (Senior)
Location: Cyprus Cyprus

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 44 (Total in Forum: 44) (Refresh)FirstPrevNext
QuestionNO Connection Could Be Made Because the Target Machine Actively Refused Itmemberoerslaafroze6:37 27 Nov '07  
GeneralMIME FormatmemberAlan (Engeman)1:50 21 Sep '07  
QuestionAmbiguous referencemembertheplayer0112:33 23 Aug '07  
AnswerRe: Ambiguous referencememberChad Z. Hower aka Kudzu11:23 26 Aug '07  
QuestionNeed Helpmemberkanzz20:48 2 Aug '07  
GeneralSample for actual release of Indy.Socketsmembertim.taylor3:51 16 Jul '07  
GeneralMessage body is blankmemberPaolo Ramos7:18 29 Nov '06  
GeneralRe: Message body is blankmemberChad Z. Hower aka Kudzu8:52 29 Nov '06  
GeneralRe: Message body is blankmemberMember 39053636:45 29 Nov '08  
QuestionMessage Datesmemberecs.dp5:50 29 Aug '06  
AnswerRe: Message Datesmemberghiutzu3:37 25 Aug '08  
GeneralSSLmemberAMGG0:13 7 Aug '06  
GeneralRe: SSLmemberChad Z. Hower aka Kudzu0:45 8 Aug '06  
GeneralRe: SSLmemberNiladri Mahapatra21:43 9 Apr '07  
GeneralFirst Message always emptymemberHaggy4:00 6 Jul '06  
GeneralRe: First Message always emptymemberChad Z. Hower aka Kudzu8:40 6 Jul '06  
GeneralMIME & VS2005memberHaggy3:49 6 Jul '06  
QuestionNot working with Yahoo emailsmemberboodi_812:24 19 May '06  
GeneralHow to retrieve only unread messagesmembernewbie137:56 18 Apr '06  
GeneralRe: How to retrieve only unread messagesmemberChad Z. Hower aka Kudzu9:24 20 Apr '06  
Questionsave Attachmentmemberr78095:41 20 Feb '06  
AnswerRe: save Attachmentmembernewbie1312:24 10 Apr '06  
QuestionNon-ASCII characters?membernj0y12:24 28 Jan '06  
QuestionRe: Non-ASCII characters?memberNoClone22:27 6 Sep '07  
Question[urgent]Removing messages from servermemberbhatti812:28 10 Nov '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Jul 2005
Editor: Chris Maunder
Copyright 2005 by Chad Z. Hower aka Kudzu
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project