You are here:
ActiveXperts.com > ActiveEmail > How to Use the ActiveEmail > E-mail POP3 > Visual Basic .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 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.
Download the 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 use declare the ActiveEmail Toolkit 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 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
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
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
Following you can find the full source code which is also included in the ActiveEmail Toolkit 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 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()
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.
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.