You are here:

ActiveXperts.com > Email Component > How to Use > Pop33 > Powershell 1.0
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 the following example we are going to show how to read messages from a POP3 server using the ActiveXperts Email Component.

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 script

Create a new script using your favorite editor. You can simply use notepad. However, a Powershell editor is recommended, so you can browse through objects, objects properties and object functions.

You're now able to write a more advanced Powershell script to receive e-mail messages through POP3.

Step 3: Create ActiveXperts Email Component objects in Powershell

Create a new Powershell file called DEMO.PS1.

Insert the following line to declare and create the 'objPop3' object:

$objPop3 = new-object -comobject AxEmail.Pop3

Step 4: Gather information

The following code will ask the user for the login data of the POP3 server. It will ask for the POP3 server, the username and the password. This code will also check if its a secure connection, in case it is, it will setup the correct port number.

# Read POP3 properties
$strPop3         = ReadInput "Enter POP3 Server" "pop.gmail.com" $False 
$strPop3Account  = ReadInput "Enter POP3 Account" "" $False 
$strPop3Password = ReadInput "Enter POP3 Password" "" $False

Step 4: Receive E-mail messages

The following Powershell code will read messages from the server until all messages are read. This informaiton will be stored in the 'objPop3Mail' object.

For ($i = 1; $i -le $nAttachments; $i++)
{
  #write-host "Attachment" $i":" $objPop3Mail.GetAttachmentName($i) 
              "size:" $objPop3Mail.GetAttachmentSize($i)
  Write-host "Attachment:" $i
  write-host "  Name:" $objPop3Mail.GetAttachmentName($i)
  $strResult = "GetAttachmentName, result: " + $objPop3.LastError
  write-host $strResult 
  write-host "  Size:" $objPop3Mail.GetAttachmentSize($i)
  $strResult = "GetAttachmentSize, result: " + $objPop3.LastError
  write-host $strResult 

  # To save the attachment, call SaveAttachment
  # $objPop3Mail.SaveAttachment($i, "c:\temp\file.ext")
}    

Appendix: Full source code

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

# ***************************************************************************
#
# ActiveXperts Email Component
#
# List mailbox messages on a POP3 server
#
# (c) Copyright ActiveXperts Software - www.activexperts.com
#
# ***************************************************************************

# ***************************************************************************
# Function ReadInput
# ***************************************************************************
Function ReadInput($strPrompt, $strDefaultValue, $bAllowEmpty)
{ 
  $strReturn = ""  
  If ($strDefaultValue -ne "")
  {
     $strPrompt += " leave empty for " + $strDefaultValue
  }
  
  Do 
  {       
    write-host $strPrompt
    $strReturn = read-host
    
    If ($strReturn -eq "" -and $strDefaultValue -ne "")
    {
      $strReturn = $strDefaultValue
      write-host $strReturn
    }
    elseif ($strReturn -eq "" -and $bAllowEmpty -eq $True)
    {
      break
    }   
  } While ($strReturn -eq "") 
  
  write-host ""
  return $strReturn
}

# ***************************************************************************
# The script itself
# ***************************************************************************
cls

$objPop3 = new-object -comobject AxEmail.Pop3

# Display ActiveXperts Email Component Version
write-host "ActiveXperts Email Component Version" $objPop3.Version "; " + 
                                              "Build" $objPop3.Build "; " +
                                             "Module" $objPop3.Module

# Display if the ActiveXperts Email Component is registered.
write-host "License Status:" $objPop3.LicenseStatus
write-host ""

# Set Logfile
$objPop3.Logfile = $env:temp + "\POP3_ListMessages.txt"
write-host "Log file can be found here:"
write-host $objPop3.Logfile
write-host ""

# Read POP3 properties
$strPop3   = ReadInput "Enter POP3 Server" "pop.gmail.com" $False 
$strPop3Account  = ReadInput "Enter POP3 Account" "" $False 
$strPop3Password = ReadInput "Enter POP3 Password" "" $False

# Set secure of if necessary
If ($strPop3.ToLower() -eq "pop.gmail.com")
{
  $objPop3.SetSecure(995)
  $strResult = "SetSecure, result: " + $objPop3.LastError
  write-host $strResult 
}

if ($objPop3.LastError -eq 0)
{
  # Connect to the POP3 server
  $objPop3.Connect($strPop3, $strPop3Account, $strPop3Password)
  $strResult = "Connect, result: " + $objPop3.LastError
  write-host $strResult 
}


if ($objPop3.LastError -eq 0)
{
  # Count messages
  $nMessages = $objPop3.CountMessages()
  $strResult = "CountMessages, result: " + $objPop3.LastError
  write-host $strResult 
}

if ($objPop3.LastError -eq 0)
{
  write-host "New message(s) on server:" $nMessages

  # Iterate over all messages
  If ($nMessages -gt 0)
  {
    write-host "Listing messages..."
    For ($i = 1; $i -le $nMessages; $i++)
    {
      $objPop3Mail = $objPop3.GetEmailHeader($i)
      $strResult = "GetEmailHeader( " + $i + " ), result: " + $objPop3.LastError
      write-host $strResult 
      
      if ($objPop3.LastError -eq 0)
      {
        write-host "MessageID        : " $objPop3Mail.ID
        write-host "   From          : " $objPop3Mail.FromAddress
        write-host "   From Name     : " $objPop3Mail.FromName
        write-host "   To            : " $objPop3Mail.ToAddress
        write-host "   Subject       : " $objPop3Mail.Subject
        write-host "   Date          : " $objPop3Mail.Date  
        write-host " "
      }
    }
  }
}
  
$objPop3.Disconnect()
write-host "Disconnected."
write-host "Ready."
exit

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.