ActiveXperts SMS Component

Quicklinks

Visual Basic .NET 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 use Visual Studio 2008 to create a console application project named 'DemoApp' in a solution named 'DemoSolution'. We are going tot store this project in the directory 'C:\MyProjects'. All of these names can be changed according to your preferences. 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 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 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 the ActiveXperts SMS Component Library

Now that a new project has been created, you must add a reference to the ActiveXperts SMS Component in the project to be able to use the the ActiveXperts SMS 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 SMS Component Type Library' as shown in the following picture:

Visual Basic dotNET

(Click on the picture to enlarge)

Click 'OK' to close the 'Add Reference' dialog.

Step 4: Declare and create the ActiveXperts SMS Component objects

The following code will show you how to declare and create the GSM and SMS objects. We will use the 'objGsm' 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.

Imports Microsoft.Win32
Imports System.IO
Imports AxSms
Imports System.Threading

Module SmppProgram

  Sub Main()
    Dim objSmpp As Smpp = New Smpp()
    Dim objSmsMessage As SmsMessage = New SmsMessage()
    Dim objSmsConstants As SmsConstants = New SmsConstants()

Step 5: Gather information

The following code will ask the user for the recipient telephone number and the content of the text message. If a PIN code is required, then it will be prompted you to enter one. This data will be stored in the 'objSmsMessage' object. If any error occours while trying to open the connection, the script will end.

objSmsMessage.Clear()
objSmsMessage.ToAddress = ReadInput("Enter recipient (recommended to start with a '+')", False)
' To allow unicode (e.g. arabic, chinese), use BODYFORMAT_UNICODE
objSmsMessage.BodyFormat = objSmsConstants.BODYFORMAT_TEXT 
objSmsMessage.Body = ReadInput("Type SMS text", False)
objSmsMessage.RequestDeliveryReport = True

Step 6: 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. If any error occours the script will end.

' Submit the SMS message allow multiple-part (i.e. more than 160 chars for standard text, or more 
' tan 70 chars for Unicode text)
objSmpp.SubmitSms(objSmsMessage, objSmsConstants.MULTIPART_OK)
Console.WriteLine("SubmitSms, result: " & objSmpp.LastError.ToString)
If (objSmpp.LastError <> 0) Then
  GoTo _EndMain
End If

Appendix: Full source code

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

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

Imports Microsoft.Win32
Imports System.IO
Imports AxSms
Imports System.Threading

Module SmppProgram

  Sub Main()
    Dim objSmpp As Smpp = New Smpp() ' Create instance of COM Object
    Dim objSmsMessage As SmsMessage = New SmsMessage() ' Create instance of COM Object
    Dim objSmsConstants As SmsConstants = New SmsConstants() ' Create instance of COM Object

    Dim nServerPort As Integer = 0
    Dim strServer As String = String.Empty, 
        strSystemType As String = String.Empty, 
        strSystemID As String = String.Empty, 
        strSystemPassword As String = String.Empty
    Dim strReference As String = String.Empty

    Dim bResult As Boolean = False, bWaitForDeliveryReport As Boolean = True

    ' Set Logfile (optional, for debugging purposes)
    objSmpp.LogFile = String.Format("{0}{1}", Path.GetTempPath(), "SmppLog.txt")

    Console.WriteLine("SMS Component: Build: " & objSmpp.Build & "; " & 
                                                "Module: " & objSmpp.Module)
    Console.WriteLine(String.Empty)

    Console.WriteLine("Log file can be found here:")
    Console.WriteLine(objSmpp.LogFile)
    Console.WriteLine(String.Empty)

    ' Prompt user for SMPP server details by default, the ActiveXperts SMPP demo is used
    ReadSmppProvider(objSmpp,strServer,nServerPort,strSystemType,strSystemID,strSystemPassword)

    ' Connect to the SMPP server using host/IP-addess, a specific TCP port and a timeout
    objSmpp.Connect(strServer, nServerPort, 10000)
    Console.WriteLine(String.Format("Connect, result: {0} ({1})", objSmpp.LastError.ToString(), 
        objSmpp.GetErrorDescription(objSmpp.LastError)))
    If (objSmpp.LastError <> 0) Then
      GoTo _EndMain
    End If

    ' Bind to the SMPP server as transceiver using your SystemID(login) and password
    ' Note: although we only send, we could use SMPP_BIND_TRANSMITTER. However, we woul like to 
    ' receive a delivery report and therefore connect as SMPP_BIND_TRANSCEIVER
    objSmpp.Bind(objSmsConstants.SMPP_BIND_TRANSCEIVER, strSystemID, strSystemPassword, 
        strSystemType, objSmsConstants.SMPP_VERSION_34, 0, 0, String.Empty, 10000)
    Console.WriteLine(String.Format("Bind, result: {0} ({1})", objSmpp.LastError.ToString, 
        objSmpp.GetErrorDescription(objSmpp.LastError)))
    If (objSmpp.LastError <> 0) Then
      GoTo _EndMain
    End If

    objSmsMessage.Clear()
    objSmsMessage.ToAddress = ReadInput("Enter recipient (start with a '+')", False)
    ' To allow unicode (e.g. arabic, chinese), use BODYFORMAT_UNICODE
    objSmsMessage.BodyFormat = objSmsConstants.BODYFORMAT_TEXT 
    objSmsMessage.Body = ReadInput("Type SMS text", False)
    objSmsMessage.RequestDeliveryReport = True

    ' Submit the SMS message allow multiple-part (i.e. more than 160 chars for 
    ' standard text, or more tan 70 chars for Unicode text)
    objSmpp.SubmitSms(objSmsMessage, objSmsConstants.MULTIPART_OK)
    Console.WriteLine("SubmitSms, result: " & objSmpp.LastError.ToString)
    If (objSmpp.LastError <> 0) Then
      GoTo _EndMain
    End If

    'WAit for Update
    Do While (objSmpp.WaitForSmsUpdate(1000))
      objSmsMessage = objSmpp.FetchSmsUpdate()
      Console.WriteLine("FetchSmsUpdate: " & objSmpp.LastError.ToString)
      If (objSmpp.LastError = 0) Then
        Exit Do
      End If
    Loop

    ' Get the message reference , that will be matched against a delivery report 
    ' (still to be received) later on
    strReference = objSmsMessage.Reference
    Console.WriteLine("Message Reference (can be used with status reports):" & strReference)
    If (strReference = String.Empty) Then
      GoTo _EndMain
    End If

    If (bWaitForDeliveryReport) Then
      Console.WriteLine("Waiting for delivery report...")
    End If

    While (bWaitForDeliveryReport)
      Dim objMessage As SmsMessage

      ' Delivery Reports are incoming SMS messages with property SmppIsDeliveryReport 
      ' set to true!
      objMessage = objSmpp.ReceiveMessage()
      Console.WriteLine("ReceiveMessage: " & objSmpp.LastError.ToString)


      If (objSmpp.LastError = 0) Then
        If (objMessage.SmppIsDeliveryReport) Then
          Console.WriteLine("Delivery Report found.")
          Console.WriteLine(String.Format("Status ({0}): {1}", strReference, 
              objMessage.Body.Substring(objMessage.Body.IndexOf("stat:") + 5, 7)))
          bWaitForDeliveryReport = False
        Else
          Console.WriteLine("  Message to: " & objMessage.ToAddress)
          Console.WriteLine("  Message body: " & objMessage.Body)
        End If
      End If

      If (bWaitForDeliveryReport) Then
        Console.WriteLine("No delivery report received yet.")
        Thread.Sleep(3000)
      End If
    End While

_EndMain:
    ' Unbind and Disconnect
    objSmpp.Unbind()
    objSmpp.Disconnect()

    Console.WriteLine("Disconnected.")
    Console.WriteLine("Ready.")
    Console.WriteLine("Press <ENTER> to continue.")
    Console.ReadLine()
  End Sub

  Function ReadRecipient()
    Dim strInput As String = String.Empty
    Dim strRecipient As String = String.Empty

    Console.WriteLine("Enter recipient (must start with a '+') : ")
    While (strRecipient = String.Empty)

      Console.Write("  > ")
      strInput = Console.ReadLine()

      If (strInput = String.Empty) Then
        Continue While
      End If

      If (strInput.Length > 1 And strInput(0) = "+") Then
        strRecipient = strInput
      End If
    End While
    Console.WriteLine("  Recipient: " & strRecipient & vbCrLf)

    Return strRecipient
  End Function

  Sub ReadSmppProvider(ByVal objSmpp As Smpp, ByRef strServer As String, ByRef nPort As Integer, 
                       ByRef strSystemType As String, ByRef strSystemID As String, 
                       ByRef strSystemPassword As String)

    Dim strPort As String

    strServer = ReadInput("Enter SMPP provider. Type <ENTER> to use the free SMPP gateway", True)

    If (strServer = String.Empty) Then

      strServer = "smpp.activexperts-labs.com"
      nPort = 2775
      strSystemType = "SMPP"
      GetSmsDemoAccountInfo(strSystemID, strSystemPassword)

    Else
      strPort = ReadInput("Enter port. Type <ENTER> to use default port 2775", True)
      If (strPort = String.Empty) Then
        nPort = 2775

      Else
        If (Integer.TryParse(strPort, nPort) = False) Then
          nPort = 2775
        End If
      End If

      strSystemType = ReadInput("Enter SystemType. Type <ENTER> to use default SMPP", True)
      If (strSystemType = String.Empty) Then
        strSystemType = "SMPP"
      End If
      strSystemID = ReadInput("Enter SystemID (account ID to login to the provider)", False)
      strSystemPassword = ReadInput("Enter the provider's System Password", True)
    End If

    Console.WriteLine(vbCrLf & "Provider settings: ")
    Console.WriteLine("  Server: " & strServer)
    Console.WriteLine("  ServerPort: " & nPort.ToString)
    Console.WriteLine("  SystemID: " & strSystemID)
    Console.WriteLine("  SystemPassword: " & strSystemPassword)
    Console.WriteLine(String.Empty)
  End Sub

  Function GetSmsDemoAccountInfo(ByRef strGwAccount As String, ByRef strGwPassword As String)

    ' NOTE: during installation of SMS Component, a unique login and password 
    ' is created for you. This login and password can be used to send a few SMS messages 
    ' through the smpp.activexperts-labs.com gateway, for free
    ' The 'GetSmsDemoAccountInfo' reads the unique account info from a file that was 
    ' created during installation
    Dim regKey As RegistryKey
    Dim objValue As Object
    Dim strInstallRoot As String = String.Empty
    Dim strAccountFile As String = String.Empty
    Dim srReader As StreamReader

    regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\ActiveXperts\\
SMS Component", False)

    objValue = regKey.GetValue("InstallRoot")

    If (Not objValue = Nothing) Then

      strInstallRoot = objValue.ToString()

      strAccountFile = strInstallRoot
      strAccountFile += "\\Utilities\\activexperts-labs.txt"
      If (Not File.Exists(strAccountFile)) Then
        Return False
      End If

      srReader = File.OpenText(strAccountFile)
      srReader.ReadLine()                 ' 1st line is a comment line; skip it
      strGwAccount = srReader.ReadLine()  ' 2nd line is the account
      strGwPassword = srReader.ReadLine() ' 3rd line is the password
    End If

    regKey.Close()

    Return True
  End Function

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

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

    Console.WriteLine(strTitle)
    Do
      Console.Write("  > ")
      strInput = Console.ReadLine()
      If (strInput.Length > 1) Then
        strReturn = strInput
      End If
    Loop While (strReturn = String.Empty And Not bAllowEmpty)

    Return strReturn
  End Function

End Module

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.