ActiveXperts SMS Component

Quicklinks

Visual C# .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 C# 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 C# .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:

SMS using Visual C# .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:

SMS using Visual C# .NET

(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 SMPP 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.

namespace SmppProgram
{
  using System;
  using System.IO;
  using System.Threading;
  using AxSms;
  using Microsoft.Win32;

  class SmppProgram
  {
    static void Main(string[] args)
    {
      Smpp objSmpp = new Smpp();
      SmsMessage objSmsMessage = new SmsMessage();
      SmsConstants objSmsConstants = 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. This data will be stored in the 'objSmsMessage' object.

// Message: set all properties
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("Enter the message text you want to send to the recipient", false);
objSmsMessage.RequestDeliveryReport = true;

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.

// Submit the SMS message; allow multiple-part (i.e. more than 160 chars 
// for standard text, or more tan 70 chars for Unicode text)
obj = objSmsMessage;
objSmpp.SubmitSms(ref obj, objSmsConstants.MULTIPART_OK);
Console.WriteLine( "SubmitSms, result: " + objSmpp.LastError );

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.cs" company="ActiveXperts">
//     Copyright (c) ActiveXperts Software - www.activexperts.com
// </copyright>
// <author>ActiveXperts</author>
//-----------------------------------------------------------------------

namespace SmppProgram
{
  using System;
  using System.IO;
  using System.Threading;
  using AxSms;
  using Microsoft.Win32;

  class SmppProgram
  {
    static void Main(string[] args)
    {
      Smpp objSmpp = new Smpp();
      SmsMessage objSmsMessage = new SmsMessage();
      SmsConstants objSmsConstants = new SmsConstants();

      int nServerPort = 0;
      string strServer = string.Empty, 
             strSystemType = string.Empty, 
             strSystemID = string.Empty, 
             strSystemPassword = string.Empty;
      string strReference = string.Empty;
      object obj;
      bool bWaitForDeliveryReport = true;

      // Set Logfile (optional, for debugging purposes)
      objSmpp.LogFile = 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, ref strServer, ref nServerPort, ref strSystemType, 
          ref strSystemID, ref 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, 
          objSmpp.GetErrorDescription(objSmpp.LastError)));
      if (objSmpp.LastError != 0)
        goto _EndMain;

      // 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 would 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, 
                objSmpp.GetErrorDescription(objSmpp.LastError)));
      if (objSmpp.LastError != 0)
        goto _EndMain;

      // Message: set all properties
      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("Enter the message text you want to send", 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)
      obj = objSmsMessage;
      objSmpp.SubmitSms(ref obj, objSmsConstants.MULTIPART_OK);
      Console.WriteLine( "SubmitSms, result: " + objSmpp.LastError );
      if (objSmpp.LastError != 0)
        goto _EndMain;

       // Wait for Update
      while (objSmpp.WaitForSmsUpdate(1000))
      {
        objSmsMessage = (SmsMessage)objSmpp.FetchSmsUpdate();
        Console.WriteLine(string.Format("FetchSmsUpdate, result: {0} ({1})", objSmpp.LastError));
        if (objSmpp.LastError == 0)
        {
          break;
        }
      }
     
      // 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)
        goto _EndMain;

      if (bWaitForDeliveryReport)
        Console.WriteLine("Waiting for delivery report...");

      while (bWaitForDeliveryReport)
      {
        SmsMessage objMessage;

        // Delivery reports are incoming SMS messages with property 
        // SmppIsDeliveryReport set to true!
        objMessage = (SmsMessage)objSmpp.ReceiveMessage();
        Console.WriteLine("ReceiveMessage, result: " + objSmpp.LastError);

        if (objSmpp.LastError == 0 && objMessage.SmppIsDeliveryReport)
        {
          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 if (objSmpp.LastError == 0)
        {
          Console.WriteLine("  Message to: " + objMessage.ToAddress);
          Console.WriteLine("  Message body: " + objMessage.Body);
        }

        if (bWaitForDeliveryReport)
        {
          Console.WriteLine("No delivery report received yet.");
          Thread.Sleep(3000);
        }
      }

_EndMain:
      // Unbind and Disconnect
      objSmpp.Unbind();
      objSmpp.Disconnect();

      Console.WriteLine("Disconnected.");
      Console.WriteLine("Ready.");
      Console.WriteLine("Press <ENTER> to continue.");
      Console.ReadLine();
    }

    static private string ReadInput( string strTitle, bool bAllowEmpty )
    {
      string strInput;
      Console.WriteLine(strTitle + ": ");
      do
      {
        Console.Write("  > ");
        strInput = Console.ReadLine();
      }
      while (strInput == string.Empty && !bAllowEmpty);
      return strInput;
    }

    static private void ReadSmppProvider(Smpp objSmpp, ref string strServer, ref int nPort, 
        ref string strSystemType, ref string strSystemID, ref string strSystemPassword)
    {
      string strPort;

      strServer = ReadInput("Enter hostname of SMPP server. 
Type <ENTER> to use the free SMPP gateway", true);

      if (strServer == string.Empty)
      {
        strServer = "smpp.activexperts-labs.com";
        nPort = 2775;
        strSystemType = "SMPP";
        GetSmsDemoAccountInfo(ref strSystemID, ref strSystemPassword);                
      }
      else
      {
        strPort = ReadInput("Enter portnumber of SMPP server. 
Type <ENTER> to use default port 2775", true);
        if (strPort == string.Empty)
            nPort = 2775;
        else
            nPort = int.TryParse(strPort, out nPort) ? nPort : 2775;

        strSystemType = ReadInput("Enter SystemType. Type <ENTER> to use default SMPP", true);
        if (strSystemType == string.Empty)
          strSystemType = "SMPP";
        strSystemID = ReadInput("Enter account systemID", false);
        strSystemPassword = ReadInput("Enter account password", true);
      }

      Console.WriteLine("\r\n" + "Provider settings: ");
      Console.WriteLine("  Server: " + strServer);
      Console.WriteLine("  ServerPort: " + nPort);
      Console.WriteLine("  SystemID: " + strSystemID);
      Console.WriteLine("  SystemPassword: " + strSystemPassword);
      Console.WriteLine(string.Empty);
    }

    private static bool GetSmsDemoAccountInfo(ref string strGwAccount, ref string strGwPassword)
    {
      // 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

      RegistryKey regKey;

      object objValue = null;

      string strInstallRoot = string.Empty;
      string strAccountFile = string.Empty;
      StreamReader r;

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

      objValue = regKey.GetValue("InstallRoot");

      if (objValue != null)
      {
        strInstallRoot = objValue.ToString();

        strAccountFile = strInstallRoot;
        strAccountFile += "\\Utilities\\activexperts-labs.txt";
        if (!File.Exists(strAccountFile))
          return false;

        r = File.OpenText(strAccountFile);
        r.ReadLine();                 // 1st line is a comment line; skip it
        strGwAccount = r.ReadLine();  // 2nd line is the account
        strGwPassword = r.ReadLine(); // 3rd line is the password
      }

      regKey.Close();

      return true;
    }
  }
}

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.