ActiveXperts SMS Component

Quicklinks

Delphi sample - Send SMS text through an HTTP 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 Delphi console application project named 'httpsend.dpr' 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 HTTP 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 Delphi Project

Launch Delphi from the Start menu. Choose 'New' from the 'File' menu and select: 'Console Application'.

Delphi Pascal

(Click on the picture to enlarge)

Step 3: Refer to the ActiveXperts SMS Component Library and create the objects

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 ActiveXperts SMS Component objects. To do so, choose 'Import Component...' from the 'Component' menu. The Import Components' dialog appears. Select 'Import a Type Library':

Delphi Pascal

(Click on the picture to enlarge)

In the 'Registered Type Libraries' page, select ActiveXperts SMS Component Type Library' and click 'Next':

Pascal

(Click on the picture to enlarge)

In the 'Components' page, leave all fields default and click 'Next':

Delphi

(Click on the picture to enlarge)

In the 'Install' page, select 'Create Unit' and click 'Next':

Borland Delphi

(Click on the picture to enlarge)

The interface code is generated now and is shown in the AxSms_TLB tab of the project.

Step 4: Declare and create the object

From the Project Manager, open Unit1.bas and add the AxSms_TLB to the 'Uses' statement to refer to the ActiveXperts SMS Component library:

Borland Delphi

(Click on the picture to enlarge)

In the 'private' or 'public' section, declare the following objects:

objHttp      : Http;
objConstants : Constants;
objOleVariant: OleVariant;
objSmsMessage: Message;

You can now create the objects, for instance in the 'FormCreate' function:

objHttp       := CoHttp.Create;
objConstants  := CoSmsConstants.Create;
objSmsMessage := coSmsMessage.Create;

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.

strResult := objHttp.SendSms(objOleVariant, objConstants.MULTIPART_OK);

Appendix: Full source code

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

program httpsend;

{$APPTYPE CONSOLE}

uses
  SysUtils, registry, ActiveX, Windows,
  AxSms_TLB in '..\typelib-imports\AxSms_TLB.pas';

var
 objHttp: Http;
 objConstants: SmsConstants;
 objOleVariant: OleVariant;
 objSmsMessage: SmsMessage;
 strTempUsername: String;
 strTempPassword: String;
 strUsername: WideString;
 strPassword: WideString;
 strToAddress: WideString;
 strBody: WideString;
 strResult: WideString;
 strLogPath: string;
 arrTempPath: array[0..MAX_PATH] of Char;
 objReg: TRegistry;
 strInstallRoot: String;
 objFile: TextFile;


begin
  objHttp := CoHttp.Create;
  objConstants := CoSmsConstants.Create;
  objSmsMessage := coSmsMessage.Create;
  objReg := TRegistry.Create;

  // Display version information about the ActiveXperts SMS Component
  Writeln('SMS Component Version ' + objHttp.Version +
    '; Build ' + objHttp.Build + '; Module ' + objHttp.Module);
  Writeln;

  // Set a log file. This will log all communication with the GSM device
  GetTempPath(MAX_PATH, arrTempPath);
  strLogPath := StrPas(arrTempPath);
  strLogPath := strLogPath + 'gsmsend.log';
  objHttp.LogFile := strLogPath;
  Writeln('Log file set to: ' + strLogPath);
  Writeln;

  // Get Install Root from registry
  objReg.RootKey := HKEY_LOCAL_MACHINE;
  if (objReg.OpenKey('SOFTWARE\ActiveXperts\SMS Component', False) = False) then
      strInstallRoot := objReg.ReadString('InstallRoot');
  objReg.CloseKey;
  objReg.Free;

  // Read Username and Password from file
  AssignFile(objFile, strInstallRoot + '\Utilities\activexperts-labs.txt');
  try
    Reset(objFile);
    ReadLn(objFile);
    ReadLn(objFile, strTempUsername);
    ReadLn(objFile, strTempPassword);
    CloseFile(objFile);
  except
    writeln('Warning, could not find the ActiveXperts Demo Gateway credentials.');
  end;

  // Get SystemId
  Writeln('Enter account systemID: (press enter to use ' + strTempUsername + ')');
  Readln(strUsername);
  if (strUsername = '') then
    strUsername := strTempUsername;

  // Get Password
  Writeln('Enter account password: (press enter to use ' + strTempPassword + ')');
  Readln(strPassword);
  if (strPassword = '') then
    strPassword := strTempPassword;

  // Set ProviderHost
  objHttp.Url := 'post.activexperts-labs.com:8080/sendsms/default.asp' +
    '?username=' + strUsername +
    '&password=' + strPassword +
    '&text=' + objConstants.HTTP_PLACEHOLDER_BODY +
    '&to=' + objConstants.HTTP_PLACEHOLDER_TOADDRESS;

  // Get Recipient
  Writeln('Enter recipient address:');
  Readln(strToAddress);
  Writeln;

  // Get Body
  Writeln('Enter the message');
  Readln(strBody);
  Writeln;

  // Sms Message Properties
  objSmsMessage.BodyFormat := objConstants.GSM_MESSAGEFORMAT_TEXT;
  objSmsMessage.ToAddress := strToAddress;
  objSmsMessage.Body := strBody;

  objOleVariant := OleVariant(objSmsMessage);
  strResult := objHttp.SendSms(objOleVariant, objConstants.MULTIPART_OK);

    // Show error if we could not open the device
  if (objHttp.LastError <> 0) then
    begin
      Writeln('Unable to send message: ' +
        objHttp.GetErrorDescription(objHttp.LastError));
      Writeln('Press ENTER to close the program');
      Readln;
      Exit;
    end
  else
    begin
      Writeln(strResult);
      Writeln;
      Writeln('Result: ' + IntToStr(objHttp.LastError) + ' (' +
        objHttp.GetErrorDescription(objHttp.LastError) + ')');
      Writeln('Response: ' + IntToStr(objHttp.LastResponseCode));
    end;
  Writeln;

  // Close the device and exit to the command prompt
  Writeln('Press ENTER to close the program');
  Readln;
end.

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.