|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIn 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 codetry {
// 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 Running the demoIt 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. OutputRunning 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.
|
||||||||||||||||||||||