ActiveXperts SMS Component

Quicklinks

Visual C# .NET sample - Send SMS text through a COM/USB GSM modem

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 GSM modem is required for this demo.

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

VisualStudio create new project

(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:

VisualStudio C#.NET add reference

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

namespace GsmProgram
{
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Threading;
  using AxSms;

  class GsmProgram
  {
    static void Main(string[] args)
    {
      Gsm objGsm = new Gsm();                           
      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. If a PIN code is required, then it will be prompted you to enter one. This data will be stored in the 'objSmsMessage' object.

' Message Settings
objSmsMessage.Clear()
objSmsMessage.ToAddress = Request("txtToAddress")
objSmsMessage.Body = Request("txtBody")
objSmsMessage.BodyFormat = objSmsConstants.BODYFORMAT_TEXT

Step 6: Send the SMS message

The following code shows how to send an SMS message using the data that was stored in the 'objSmsMessage' and 'objSmsConstants' objects. The timeout value is set to 25 seconds, which means that the modem will try to send the message for 25 seconds, after this value it will stop trying.

// Send the message !  
obj = objSmsMessage;
objGsm.SendSms(ref obj, objSmsConstants.MULTIPART_OK, 25000);
Console.WriteLine("SendSms, result: " + 
                   objGsm.LastError + " (" + 
                   objGsm.GetErrorDescription(objGsm.LastError) + ")");
if (objGsm.LastError != 0)
  goto _EndMain;

Appendix: Full source code

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

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

namespace GsmProgram
{
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Threading;
  using AxSms;

  class GsmProgram
  {       
    static void Main(string[] args)
    {            
      Gsm objGsm = new Gsm(); // Create instance of COM Object
      SmsMessage objSmsMessage = new SmsMessage(); // Create instance of COM Object
      SmsConstants objSmsConstants = new SmsConstants(); // Create instance of COM Object
      GsmDeliveryReport objStatusReport = null;
      object obj;
      string strReference, strDevice;

      // Set Logfile (optional, for debugging purposes)
      objGsm.LogFile = Path.GetTempPath() + "GsmLog.txt";

      Console.WriteLine("SMS Component: Build: " + objGsm.Build + "; " +
                                                  "Module: " + objGsm.Module);
      Console.WriteLine(string.Empty);

      Console.WriteLine("Log file can be found here:");
      Console.WriteLine(objGsm.LogFile);
      Console.WriteLine(string.Empty);

      if ((strDevice = ReadDevice(objGsm)) == string.Empty)
        goto _EndMain;   

      objGsm.Open(strDevice, string.Empty, 0);
      Console.WriteLine("Open, result: " + 
                         objGsm.LastError + " (" + 
                         objGsm.GetErrorDescription(objGsm.LastError) + ")");
      // 36101 means: modem requires pin code, see also: www.activexperts.com/support/errorcodes
      if (objGsm.LastError == 36101) 
      {
        objGsm.Open(strDevice, ReadInput("Enter PIN code", false), 0);
        Console.WriteLine("Open, result: " + 
                           objGsm.LastError + " (" + 
                           objGsm.GetErrorDescription(objGsm.LastError) + ")");       
      }

      if (objGsm.LastError != 0)
        goto _EndMain; 

      // Message settings
      objSmsMessage.ToAddress = ReadInput("Enter recipient: ", false);
      objSmsMessage.BodyFormat = objSmsConstants.BODYFORMAT_TEXT;
      objSmsMessage.Body = ReadInput("Enter message body: ", false);
      objSmsMessage.RequestDeliveryReport = ReadYesNo("Do you want a status report (y/n)? ");
      
      bool bSearchStatusReport = objSmsMessage.RequestDeliveryReport;
      
      // Send the message !  
      obj = objSmsMessage;
      objGsm.SendSms(ref obj, objSmsConstants.MULTIPART_OK, 25000);
      Console.WriteLine("SendSms, result: " + 
                         objGsm.LastError + " (" + 
                         objGsm.GetErrorDescription(objGsm.LastError) + ")");
      if (objGsm.LastError != 0)
        goto _EndMain;

      objSmsMessage = (SmsMessage)obj;
      strReference = objSmsMessage.Reference;
      Console.WriteLine("Message Reference (can be used with status reports): " + strReference);

      // Status Report
      while (bSearchStatusReport)
      {
        objGsm.Receive(objSmsConstants.GSM_MESSAGESTATE_ALL, false, 
          objSmsConstants.GSM_STORAGETYPE_ALL, 25000);
        Console.WriteLine("Receive, result: " + 
                           objGsm.LastError + " (" + 
                           objGsm.GetErrorDescription(objGsm.LastError) + ")");

        if (objGsm.LastError != 0)
        {
          break;
        }

        objStatusReport = (GsmDeliveryReport)objGsm.GetFirstReport();
        
        while (objGsm.LastError == 0)
        {
          if (objStatusReport.Reference == strReference)
          {
            bSearchStatusReport = false;
            Console.WriteLine(string.Format("Status Report {0} received!", 
              objStatusReport.Reference));
            obj = objStatusReport;
            objGsm.DeleteReport(ref obj);
            Console.WriteLine("DeleteReport, result: " + 
                               objGsm.LastError + " (" + 
                               objGsm.GetErrorDescription(objGsm.LastError) + ")");
            break;
          }
          else
          {
            objStatusReport = (GsmDeliveryReport)objGsm.GetNextReport();                        
          }
        }

        if (bSearchStatusReport)
          Console.WriteLine("Status report not found yet.");
        Thread.Sleep(3000);                
      }

_EndMain:            
      objGsm.Close(); // Make sure the port gets closed
      Console.WriteLine("Ready.");
      Console.WriteLine("Press <ENTER> to continue.");
      Console.ReadLine();
    }

    static private bool ReadYesNo(string strTitle)
    {
      string strInput;
      Console.Write(strTitle);
      strInput = Console.ReadLine();
      Console.WriteLine();
      return strInput.Length > 0 && (strInput[0] == 'y' || strInput[0] == 'Y');
    }

    static private string ReadInput(string strTitle, bool bAllowEmpty)
    {
      string strInput, strReturn = string.Empty;

      Console.WriteLine(strTitle);
      do
      {
        Console.Write("  > ");
        strInput = Console.ReadLine();
        if (strInput.Length > 1)
          strReturn = strInput;
      } while (strReturn == string.Empty && !bAllowEmpty);

      return strReturn;
    }

    static private string ReadDevice(Gsm objGsm)
    {
      string strInput, strDevice, strPort = string.Empty;
      string strMessage = string.Empty;
      int i = 0;
      List<string> lsDeviceNames = new List<string>();

      strMessage += "Select a device: ";            

      strDevice = objGsm.FindFirstDevice();

      while (objGsm.LastError == 0)
      {
        strMessage += "\r\n  " + (i).ToString() + ": " + strDevice;
        lsDeviceNames.Add(strDevice);
        strDevice = objGsm.FindNextDevice();
        i++;
      }

      // Add COM ports.
      // Gets first COM port            
      strPort = objGsm.FindFirstPort();
      while (objGsm.LastError == 0)
      {
        strMessage += "\r\n  " + (i).ToString() + ": " + strPort;
        lsDeviceNames.Add(strPort);
        // Gets next COM port
        strPort = objGsm.FindNextPort();
        i++;
      }

      strDevice = string.Empty;

      if (lsDeviceNames.Count == 0)
      {
        Console.WriteLine("No devices or COM ports where found.");             
      }
      else
      {
        Console.WriteLine(strMessage);             

        while (strDevice == string.Empty)
        {
          Console.Write("  > ");
          strInput = Console.ReadLine();

          int iDevice = int.TryParse(strInput, out iDevice) ? iDevice : 0;
          if (iDevice > lsDeviceNames.Count - 1) iDevice = 0; 
          strDevice = lsDeviceNames[iDevice];
        }
        Console.WriteLine("  Selected device: " + strDevice + "\r\n");
      }
      return strDevice;
    }
  }
}

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.