ActiveXperts SMS Component

Quicklinks

Powershell sample - Send SMS text through an SMPP provider

The ActiveXperts SMS Component is a software development kit (SDK) to enhance an application or script with SMS or Pager functionality. SMS messages can be sent/received using a GSM modem, an SMPP provider or an HTTP compliant SMSC.


Introduction

In this example we are going to create a Powershell Script to send SMS messages. This demo project will ask the user to give a phone number and a message body on the command prompt.

A subscription to an SMPP provider is required. For this demo you can send a limited number of messages through our own gateway.

Step 1: Download and install The ActiveXperts SMS Component

Download the ActiveXperts SMS 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/receive SMS using SMS Component.

Step 3: Declare and create the ActiveXperts SMS Component objects

Create a new Powershell file called DEMO.PS1.

The following code will show you how to declare and create the GSM and SMS objects. We will use the 'objSmpp' object to send the message itself. The 'objSmsMessage' object will be used to store information of the message and the 'objSmsConstants' object containes constant values releated to the SMS objects.

Insert the following line to declare and create the GSM object:

$objSmpp         = new-object -comobject AxSms.Smpp
$objMessage      = new-object -comobject AxSms.Message
$objSmsConstants = new-object -comobject AxSms.Constants

Step 4: Gather information

The following code will ask the user for the recipient telephone number and the content of the text message. This data will be stored in the 'objSmsMessage' object. If any error occours while trying to open the connection, the script will end.

# Set SMPP Server
$strServer    = ReadInput "Enter hostname of SMPP server" "smpp.activexperts-labs.com" $False

# Set SMPP ServerPort
$nPort        = ReadInput "Enter portnumber of SMPP server" "2775" $False

# Set SMPP SystemID
$strUsername  = ReadInput "Enter account systemID" $strSystemID $False 

# Set SMPP Password
$strPassword  = ReadInput "Enter account password" $strPassword $False

Step 5: Send SMS messages

The following code shows how to send an SMS message using the data that was stored in the 'objSmsMessage' and 'objSmsConstants' objects. After the message is sent the error result will be displayed. Error 0 means the message was sent succesfully.

# Send the message
write-host "Sending the message..."

$objSmpp.SubmitSms($objMessage)
$strResult = "SubmitSms, result: " + $objSmpp.LastError
write-host $strResult

Appendix: Full source code

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

# ***************************************************************************
#
# ActiveXperts SMS Component
#
# Send a text SMS message through a SMPP provider.
#
# (c) Copyright ActiveXperts Software - www.activexperts.com
#
# ***************************************************************************

# ***************************************************************************
# Function GetSmsAccountInfo
# ***************************************************************************
Function GetSmsAccountInfo([ref]$strSystemID, [ref]$strPassword)
{
  $objRegistery = new-object -comobject WScript.Shell
  $objFileSystem = new-object -comobject Scripting.FileSystemObject
  $strInstallRoot = $objRegistery.RegRead("HKLM\SOFTWARE\ActiveXperts\" + 
                                          "SMS Component\InstallRoot")
  
  If ($strInstallRoot -ne "")
  {
    $strAccountFile = $strInstallRoot + "\Utilities\activexperts-labs.txt"
    
    If ($objFileSystem.FileExists($strAccountFile) -ne $True)
    {
      write-host "Account details for ActiveXperts Demo gateway not found."    
      exit
    }
    else
    {
      $objFile = $objFileSystem.OpenTextFile($strAccountFile)
      $Dummy =              $objFile.ReadLine() # 1st line is a comment line; skip it
      $strSystemID.Value =  $objFile.ReadLine() # 2nd line is the SystemID
      $strPassword.Value =  $objFile.ReadLine() # 3rd line is the password
      $objFile.Close()
    } 
  }
}

# ***************************************************************************
# 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

$objSmpp = new-object -comobject AxSms.Smpp
$objMessage = new-object -comobject AxSms.Message
$objSmsConstants = new-object -comobject AxSms.Constants

# Display SMS Component Version
write-host "SMS Component Version" $objSmpp.Version "; Build" $objSmpp.Build "; " + 
                                                                "Module" $objSmpp.Module

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

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

$strSystemID = ""
$strPassword = ""
GetSmsAccountInfo ([ref]$strSystemID) ([ref]$strPassword)

# Set SMPP Server
$strServer    = ReadInput "Enter hostname of SMPP server" "smpp.activexperts-labs.com" $False

# Set SMPP ServerPort
$nPort        = ReadInput "Enter portnumber of SMPP server" "2775" $False

# Set SMPP SystemID
$strUsername  = ReadInput "Enter account systemID" $strSystemID $False 

# Set SMPP Password
$strPassword  = ReadInput "Enter account password" $strPassword $False

$nTimeout = 2000

# Connect to SMPP provider
$objSmpp.Connect($strServer, $nPort, $nTimeout)
$strResult = "Connect, result: " + $objSmpp.LastError
write-host $strResult

If ($objSmpp.LastError -ne 0)
{
  exit
}

$objSmpp.Bind($objSmsConstants.SMPP_BIND_TRANSCEIVER, 
              $strUsername, 
              $strpassword, "", 
              $objSmsConstants.SMPP_VERSION_34, 0, 0, "", 
              $nTimeout)
$strResult = "Bind, result: " + $objSmpp.LastError
write-host $strResult

If ($objSmpp.LastError -ne 0)
{
  $objSmpp.Disconnect
  exit
}

# Message: set all properties
$objMessage.Clear()
$objMessage.ToAddress        = ReadInput "Enter Recipient" "" $False
$objMessage.Body             = ReadInput "Enter the message text you want to send" "Hello" $False
$objMessage.BodyFormat       = $objSmsConstants.BODYFORMAT_TEXT   
$objMessage.RequestDeliveryReport = $True

# Send the message
write-host "Sending the message..."

$objSmpp.SubmitSms($objMessage)
$strResult = "SubmitSms, result: " + $objSmpp.LastError
write-host $strResult

If ($objSmpp.LastError -ne 0)
{
  $objSmpp.Unbind()
  $objSmpp.Disconnect()
  exit
}

While ($objSmpp.WaitForSmsUpdate(1000) -eq $True)
{
  $objMessage = $objSmpp.FetchSmsUpdate()
  $strResult = "FetchSmsUpdate, result: " + $objSmpp.LastError
  write-host $strResult
  
  if ($objSmpp.LastError -eq 0)
  {
    break;
  }  
} 

$strReference = $objMessage.Reference

# Show the result
If ($objSmpp.LastError -ne 0)
{
  $objSmpp.Unbind()
  $objSmpp.Disconnect()
  exit
}

# Show the Message Reference
write-host "Message successfully submitted" 
write-host "Message reference :"$strReference

$bGo = $True
While ($bGo)
{
  $objMessage = $objSmpp.ReceiveMessage()
  $strResult = "ReceiveMessage, result: " + $objSmpp.LastError
  write-host $strResult
  
  While ($objSmpp.lastError -eq 0)
  {
    If ($objMessage.SmppIsDeliveryReport -eq $True)
    {
  write-host "Delivery rpt for:" $objMessage.Body.SubString($objMessage.Body.IndexOf("id:") + 3,8)
      write-host "State:" $objMessage.Body.SubString($objMessage.Body.IndexOf("stat:") + 5,7)
      $bGo = $False
      break
    }
    else
    {
      write-host " Message to:" $objMessage.ToAddress
      write-host " Message body:" $objMessage.Body
    }
    
    $objMessage = $objSmpp.ReceiveMessage()
    $strResult = "ReceiveMessage, result: " + $objSmpp.LastError
    write-host $strResult
  }
   
  If ($bGo)
  {
    write-host "No delivery report received yet."
    sleep 3
  }
}

$objSmpp.Unbind()
$objSmpp.Disconnect()

write-host "Disconnected"
write-host "Ready."

You can download the full source code of this project from the ActiveXperts FTP site: ftp.activexperts-labs.com/samples/sms-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 SMS 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.