You are here:

ActiveXperts.com > Email Component > How to Use > Pop33 > Visual Basic .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 Visual Basic.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. In this demo we will connect and receive messages from a POP3 server. The user will be asked to enter the login and password details of the 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 Basic .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:

Visual Basic .NET

(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:

Visual Basic.NET

(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 use declare ActiveXperts Email Component objects. The 'objPop3' will store information related to the POP3 connection. The 'objPop3Mail' will contain the information of each e-mail message and 'objConstants' containes information related to the e-mail settings for POP3.

Imports AxEmail
Imports System.IO

Module Pop3Program

  Sub Main()
    Dim objPop3 As Pop3 = New Pop3
    Dim objPop3Mail As EMailMessage = Nothing
    Dim objConstants As EMailConstants = New EMailConstants

Step 5: Gather information

The following code will ask the user to enter the information for the POP3 server. It will also ask the user if it is or not a secure server, if it is it will set the 'objPop3' object to use the port number 995;

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()
Loop Until (strYN.StartsWith("Y") Or strYN.StartsWith("N"))

If (strYN.StartsWith("Y")) Then
  objPop3.SetSecure(995)
  Console.WriteLine("SetSecure, result: " & objPop3.LastError.ToString() )
End If

Step 6: List all messages

The following code will count the messages on the POP3 server, then it will download and display all the messages until all messages have been processed:

' List all messages
Console.WriteLine("Listing messages...")
For i = 1 To numMessages

  objPop3Mail = objPop3.GetEmailHeader(i)
  Console.WriteLine("GetEmailHeader, result: " & objPop3.LastError.ToString() )
  If (objPop3.LastError = 0) Then
    Console.WriteLine("MessageID        : " & objPop3Mail.ID)
    Console.WriteLine("   From          : " & objPop3Mail.FromAddress)
    Console.WriteLine("   From Name     : " & objPop3Mail.FromName)
    Console.WriteLine("   To            : " & objPop3Mail.ToAddress)
    Console.WriteLine("   Subject       : " & objPop3Mail.Subject)
    Console.WriteLine("   Date          : " & objPop3Mail.Date)
    Console.WriteLine(String.Empty)
  End If
Next

Appendix: Full source code

Following you can find the full source code which is also included in ActiveXperts Email Component package.

'-----------------------------------------------------------------------
' <copyright file="Pop3Program.vb" company="ActiveXperts">
'     Copyright (c) ActiveXperts Software - www.activexperts.com
' </copyright>
' <author>ActiveXperts</author>
'-----------------------------------------------------------------------

Imports AEmailLib
Imports System.IO

Module Pop3Program

  Sub Main()
    Dim objPop3 As Pop3 = New Pop3
    Dim objPop3Mail As EMailMessage = Nothing
    Dim objConstants As EMailConstants = New EMailConstants
    Dim strYN, strHost, strAccount, strPassword As String
    Dim i As System.Int32 = 0, numMessages As System.Int32 = 0

    ' Set Logfile (optional, for debugging purposes)
    objPop3.LogFile = String.Format("{0}{1}", 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()
    Loop Until (strYN.StartsWith("Y") Or strYN.StartsWith("N"))

    If (strYN.StartsWith("Y")) Then
      objPop3.SetSecure(995)
      Console.WriteLine("SetSecure, result: " & objPop3.LastError.ToString() )
    End If

    objPop3.Connect(strHost, strAccount, strPassword)
    Console.WriteLine("Connect, result: " & objPop3.LastError.ToString() )

    If (objPop3.LastError = 0) Then
      numMessages = objPop3.CountMessages()
      Console.WriteLine("CountMessages, result: " & objPop3.LastError.ToString() )
      Console.WriteLine("#Messages in inbox: " & numMessages.ToString())

      ' List all messages
      Console.WriteLine("Listing messages...")
      For i = 1 To numMessages

        objPop3Mail = objPop3.GetEmailHeader(i)
        Console.WriteLine("GetEmailHeader, result: " & objPop3.LastError.ToString() )
        If (objPop3.LastError = 0) Then
          Console.WriteLine("MessageID        : " & objPop3Mail.ID)
          Console.WriteLine("   From          : " & objPop3Mail.FromAddress)
          Console.WriteLine("   From Name     : " & objPop3Mail.FromName)
          Console.WriteLine("   To            : " & objPop3Mail.ToAddress)
          Console.WriteLine("   Subject       : " & objPop3Mail.Subject)
          Console.WriteLine("   Date          : " & objPop3Mail.Date)
          Console.WriteLine(String.Empty)
        End If
      Next
    End If

    objPop3.Disconnect()
    Console.WriteLine("Disconnected.")
    Console.WriteLine("Ready.")
    Console.WriteLine("Press <ENTER> to continue.")
    Console.ReadLine()

  End Sub

  Private Function ReadInput(ByVal strTitle, ByVal bAllowEmpty) As String

    Dim strInput As String = String.Empty
    Dim strReturn As String = String.Empty

    Console.WriteLine(strTitle)
    Do

      Console.Write("  > ")
      strInput = Console.ReadLine()
      If (strInput.Length > 0) Then
        strReturn = strInput
      End If
    Loop Until strReturn <> String.Empty Or bAllowEmpty

    ReadInput = strReturn
  End Function

End Module

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.