You are here:

ActiveXperts.com > Email Component > How to Use > Smtp > 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 this example we are going to create a Powershell Script to send e-mail messages. This demo project will ask the user to give a recipient e-mail address and a message body on the command prompt.

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 send e-mails using Email Component.

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 objSmtp object:

$objSmtp      = new-object -comobject AxEmail.Smtp
$objConstants = new-object -comobject AxEmail.Constants
$objMail      = new-object -comobject AxEmail.Message

Step 4: Gather information

The following code will ask the user for the recipient e-mail address and the content of the text message. You can also specify a CC or BCC recipient and you can also add an attachment to your message. This data will be stored in the 'objEmail' object.

# Mail: Clear
  $objMail.Clear()
  
# Mail: From  
# Some mail servers (including MS Exchange) require an existing mail address on that server
$objMail.FromAddress  = ReadInput "FROM e-mail address:" "" $True       

# Mail: Subject  
$objMail.Subject      = ReadInput "Subject:" "" $False
  
# Mail: Body 
$objMail.BodyPlainText  = ReadInput "Message (Plain Text):" "Hello, world!" $False
$objMail.BodyHtml       = ReadInput "Message (Html):" "Hello, world!" $False
  
# Mail: Priority  
$objMail.Priority       = $objConstants.EMAIL_MESSAGE_PRIORITY_MEDIUM # Normal (default) priority

# Mail: Encoding
$objMail.Encoding       = $objConstants.EMAIL_MESSAGE_ENCODING_DEFAULT

# Mail: To
$strRecipient = ReadInput "To e-mail address:" "" $False
$objMail.AddTo($strRecipient, $strRecipient)
$strResult = "AddTo, result: " + $objMail.LastError
write-host $strResult 
if($objMail.LastError -ne 0)
{
  exit
}

# Mail: Cc
$strRecipient = ReadInput "CC e-mail address (optional):" "" $True
if ($strRecipient -ne "")
{
  $objMail.AddCc($strRecipient, $strRecipient)
  $strResult = "AddCc, result: " + $objMail.LastError
  write-host $strResult 
}
if($objMail.LastError -ne 0)
{
  exit
}

Step 5: Send E-mail messages

The following code shows how to send an e-mail message using the data that was stored in the 'objMail' object. After the message is sent the error result will be displayed. Error 0 means the message was sent succesfully.

# Smtp: Send
$objSmtp.Send($objMail)
$strResult = "Send, result: " + $objSmtp.LastError
write-host $strResult 
write-host "Last response from SMTP Server:" $objSmtp.LastSmtpResponse 

Appendix: Full source code

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

# ********************************************************************
#
# ActiveXperts Email Component
#
# Send a simple e-mail
#
# (c) Copyright ActiveXperts Software - www.activexperts.com
#
# ********************************************************************

# ***************************************************************************
# Function ReadInput
# ***************************************************************************
Function ReadInput($strPrompt, $strDefaultValue, $bAllowEmpty)
{ 
  $strReturn = ""  
  If ($strDefaultValue -ne "")
  {
     $strPrompt += " (leave blanc 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

$objSmtp = new-object -comobject AxEmail.Smtp
$objConstants = new-object -comobject AxEmail.Constants
$objMail = new-object -comobject AxEmail.Message

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

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

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

# Mail: Clear
  $objMail.Clear()
  
# Mail: From  
# Some mail servers (including MS Exchange) require an existing mail address on that server
$objMail.FromAddress  = ReadInput "FROM e-mail address:" "" $True       

# Mail: Subject  
  $objMail.Subject      = ReadInput "Subject:" "" $False
  
# Mail: Body 
$objMail.BodyPlainText  = ReadInput "Message (Plain Text):" "Hello, world!" $False
$objMail.BodyHtml       = ReadInput "Message (Html):" "Hello, world!" $False
  
# Mail: Priority  
$objMail.Priority       = $objConstants.EMAIL_MESSAGE_PRIORITY_MEDIUM # Normal (default) priority

# Mail: Encoding
$objMail.Encoding       = $objConstants.EMAIL_MESSAGE_ENCODING_DEFAULT

# Mail: To
$strRecipient = ReadInput "To e-mail address:" "" $False
$objMail.AddTo($strRecipient, $strRecipient)
$strResult = "AddTo, result: " + $objMail.LastError
write-host $strResult 
if($objMail.LastError -ne 0)
{
  exit
}

# Mail: Cc
$strRecipient = ReadInput "CC e-mail address (optional):" "" $True
if ($strRecipient -ne "")
{
  $objMail.AddCc($strRecipient, $strRecipient)
  $strResult = "AddCc, result: " + $objMail.LastError
  write-host $strResult 
}
if($objMail.LastError -ne 0)
{
  exit
}

# Mail: Attachment
$strAttachment = ReadInput "Attachment (optional):" "" $True
if ($strAttachment -ne "")
{
  $objMail.AddAttachment($strAttachment)
  $strResult = "AddAttachment, result: " + $objMail.LastError
  write-host $strResult 
}
if($objMail.LastError -ne 0)
{
  exit
}

# Smtp: Mail Server
$strSmtpServer = ReadInput "Mail server:" "smtp.gmail.com" $False
$strSmtpAccount = ReadInput "Login (optional):" "" $True 
If( $strSmtpAccount -ne "" ) 
{
  $strSmtpPassword = ReadInput "Password:" "" $True
}

# Smtp: Secure
If( $strSmtpServer.ToLower() -eq "smtp.gmail.com" )
{
  $objSmtp.SetSecure(465)
  $strResult = "SetSecure, result: " + $objSmtp.LastError
  write-host $strResult 
}
if($objSmtp.LastError -ne 0)
{
  exit
}

# Smtp: Connect
$objSmtp.Connect($strSmtpServer, $strSmtpAccount, $strSmtpPassword)
$strResult = "Connect, result: " + $objSmtp.LastError
write-host $strResult 
if($objSmtp.LastError -ne 0)
{
  exit
}

# Smtp: Send
$objSmtp.Send($objMail)
$strResult = "Send, result: " + $objSmtp.LastError
write-host $strResult 
write-host "Last response from SMTP Server:" $objSmtp.LastSmtpResponse 
 

$objSmtp.Disconnect()

write-host "Disconnected."

write-host "Ready."

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.