You are here:

ActiveXperts.com > Email Component > How to Use > Pop33 > Visual C#.NET
ActiveXperts Email Component

Quicklinks

ActiveXperts Email Component is a software development kit (SDK) that enables the user to send (SMTP) and receive (POP3) e-mail messages. ActiveXperts Email Component 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).


Introduction

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.

Step 1: Download and install ActiveXperts Email Component

Download ActiveXperts Email Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new Visual C# .NET Project

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:

VisualStudio create new project

(Click on the picture to enlarge)

Step 3: Refer to ActiveXperts Email Component Library

Now that a new project has been created, you must add a reference to ActiveXperts Email Component in the project to be able to use the the ActiveXperts Email Component 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 Email Component Type Library' as shown in the following picture:

VisualStudio C#.NET add reference

(Click on the picture to enlarge)

Click 'OK' to close the 'Add Reference' dialog.

Step 4: Declare and create ActiveXperts Email Component objects

On top of your code, type the following line to declare ActiveXperts Email Component objects:

namespace Pop3Program
{
  using System;
  using System.Collections.Generic;
  using System.Text;
  using AxEmail;
  using System.IO;

  class Pop3Program
  {
    static void Main(string[] args)
    {
      Pop3 objPop3 = new Pop3();
      Message objMail = null;

Step 5: Gather information

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 );
}

Step 6: Receive E-mail messages

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 = (Message)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);
  }
}

Appendix: Full source code

Following you can find the full source code which is also included in ActiveXperts Email Component 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 AxEmail;
  using System.IO;

  class Pop3Program
  {
    static void Main(string[] args)
    {
      Pop3 objPop3 = new Pop3();
      Message 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 Email Component: 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 = (Message)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.

NOTE: Demo Projects are created with Microsoft Visual Studio 2008

The ActiveXperts Email Component 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.