You are here:
ActiveXperts.com > ActiveEmail > How to Use the ActiveEmail > E-mail POP3 > Visual C#.NET
Quicklinks
ActiveEmail SMTP/POP3 Toolkit is a software development kit (SDK) that enables the user to send (SMTP) and receive (POP3) e-mail messages. ActiveEmail supports SMTP, POP3, multiple recipients (To, CC, BCC), multiple attachments (ASCII and binary), rich text body formats (RTF/HTML), Unicode, multiple character sets, SMTP authorization (AUTH PLAIN, AUTH LOGIN, AUTH CRAM MD5), POP3 authorization (Plain, APOP), POP3 header download, different character sets (including arabic, chinese, japanese, russian, greek, hebrew and many more), different encodings (including 7/8 bit, quoted-printable, base64).
In this example we are going to use Visual Studio 2008 to create a console application in C#.NET project named 'DemoApp' in a solution named 'DemoSolution'. We are going to store this project in the directory 'C:\MyProjects'. All of these names can be changed according to your preferences. This demo project will ask the user to give the username and password needed to connect to the POP3 server and it will read the messages from the POP3 server.
Download the ActiveEmail Toolkit from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Launch Microsoft Visual Studio from the Start menu. Choose 'New' from the 'File' menu and click on 'Project'. In the 'New Project' dialog, select a Visual Studio template. Select a name for the application and a name for the solution. Also, select the directory where you want to store the project:
Now that a new project has been created, you must add a reference to the ActiveEmail Toolkit in the project to be able to use the the Mobile Messaging Toolkit objects. To do so, choose 'Add Reference...' from the 'Project' menu. In the 'Add Reference' dialog that pops up, select the 'COM' tab and select the ActiveXperts ActiveEmail Toolkit Type Library' as shown in the following picture:
Click 'OK' to close the 'Add Reference' dialog.
On top of your code, type the following line to declare the ActiveEmail Toolkit objects:
namespace Pop3Program
{
using System;
using System.Collections.Generic;
using System.Text;
using AxMmCtlLib;
using System.IO;
class Pop3Program
{
static void Main(string[] args)
{
Pop3 objPop3 = new Pop3();
EMailMessage objMail = null;
The following code will ask the user for the username and password of the POP3 server. It will also be asked if the server is a secure server or not, if it is port 995 will be used.
strHost = ReadInput("Enter POP3 server (IP address or host name):", false);
strAccount = ReadInput("Enter POP3 account:", false);
strPassword = ReadInput("Enter POP3 password:", false);
do
{ strYN = ReadInput("Is " + strHost + " a secure POP3 server (y/n)?", false).ToUpper(); }
// Set Secure if secure communications is required
while (!strYN.StartsWith("Y") && !strYN.StartsWith("N"));
if (strYN.StartsWith("Y"))
{
objPop3.SetSecure(995);
Console.WriteLine("SetSecure, result: " + objPop3.LastError );
}
The following code will download and display all the messages of the POP3 server:
for (i = 1; i <= numMessages; i++)
{
// Gets the Email header for the current message
objMail = (EMailMessage)objPop3.GetEmailHeader(i);
Console.WriteLine("GetEmailHeader, result: " + objPop3.LastError);
if (objPop3.LastError == 0)
{
Console.WriteLine("MessageID : " + objMail.ID);
Console.WriteLine(" From : " + objMail.FromAddress);
Console.WriteLine(" From Name : " + objMail.FromName);
Console.WriteLine(" To : " + objMail.ToAddress);
Console.WriteLine(" Subject : " + objMail.Subject);
Console.WriteLine(" Date : " + objMail.Date);
Console.WriteLine(string.Empty);
}
}
Following you can find the full source code which is also included in the ActiveEmail Toolkit package.
//-----------------------------------------------------------------------
// <copyright file="Pop3Program.cs" company="ActiveXperts">
// Copyright (c) ActiveXperts Software - www.activexperts.com
// </copyright>
// <author>ActiveXperts</author>
//-----------------------------------------------------------------------
namespace Pop3Program
{
using System;
using System.Collections.Generic;
using System.Text;
using AxMmCtlLib;
using System.IO;
class Pop3Program
{
static void Main(string[] args)
{
Pop3 objPop3 = new Pop3();
EMailMessage objMail = null;
string strHost, strAccount, strPassword, strYN;
System.Int32 i = 0, numMessages = 0;
// Set Logfile (optional, for debugging purposes)
objPop3.LogFile = Path.GetTempPath() + "Pop3Log.txt";
Console.WriteLine("ActiveXperts ActiveEmail Toolkit: Build: " + objPop3.Build +
"; Module: " + objPop3.Module);
Console.WriteLine(string.Empty);
Console.WriteLine("Log file can be found here:");
Console.WriteLine(objPop3.LogFile);
Console.WriteLine(string.Empty);
strHost = ReadInput("Enter POP3 server (IP address or host name):", false);
strAccount = ReadInput("Enter POP3 account:", false);
strPassword = ReadInput("Enter POP3 password:", false);
do
{ strYN = ReadInput("Is " + strHost + " a secure POP3 server (y/n)?", false).ToUpper(); }
// Set Secure if secure communications is required
while (!strYN.StartsWith("Y") && !strYN.StartsWith("N"));
if (strYN.StartsWith("Y"))
{
objPop3.SetSecure(995);
Console.WriteLine("SetSecure, result: " + objPop3.LastError );
}
// Connects to the POP3 server
objPop3.Connect(strHost, strAccount, strPassword);
Console.WriteLine("Connect, result: " + objPop3.LastError);
if (objPop3.LastError == 0)
{
// Counts the messages
numMessages = objPop3.CountMessages();
Console.WriteLine("CountMessages, result: " + objPop3.LastError);
Console.WriteLine("# New message(s) on server: " + numMessages.ToString());
// List all messages
Console.WriteLine("Listing messages...");
for (i = 1; i <= numMessages; i++)
{
// Gets the Email header for the current message
objMail = (EMailMessage)objPop3.GetEmailHeader(i);
Console.WriteLine("GetEmailHeader, result: " + objPop3.LastError);
if (objPop3.LastError == 0)
{
Console.WriteLine("MessageID : " + objMail.ID);
Console.WriteLine(" From : " + objMail.FromAddress);
Console.WriteLine(" From Name : " + objMail.FromName);
Console.WriteLine(" To : " + objMail.ToAddress);
Console.WriteLine(" Subject : " + objMail.Subject);
Console.WriteLine(" Date : " + objMail.Date);
Console.WriteLine(string.Empty);
}
}
}
objPop3.Disconnect();
Console.WriteLine("Disconnected.");
Console.WriteLine("Ready.");
Console.WriteLine("Press <ENTER> to continue.");
Console.ReadLine();
}
static private string ReadInput(string strTitle, bool bAllowEmpty)
{
string strInput, strReturn = string.Empty;
Console.WriteLine(strTitle);
do
{
Console.Write(" > ");
strInput = Console.ReadLine();
if (strInput.Length > 0)
strReturn = strInput;
} while (strReturn == string.Empty && !bAllowEmpty);
return strReturn;
}
}
}
You can download the full source code of this project from the ActiveXperts FTP site: ftp://ftp.activexperts-labs.com/samples/smtp-pop3-component/. There are many other working samples included with the product or on the FTP site.
The ActiveEmail Toolkit project ships with a set of Microsoft Visual Studio .NET samples. The projects are created with Microsoft Visual Studio 2008.
Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.