• Articles
  • How to send an SMS message from an appli
Published by
Aug 11, 2014 (last update: Aug 28, 2014)

How to send an SMS message from an application

Score: 3.8/5 (113 votes)
*****

How to send an SMS from a Desktop Applicaiton (written in C++).

  • Download source code - 45.7 KB


  • Introduction

    This article explains how to add the capability of sending text (SMS) messages from a desktop application.


    Background

    The article focuses on an implementation using MFC / C++. While looking for a reliable and cheap solution for sending SMS messages programmatically, I came across a company named CardBoardFish which covers 150 countries and provides an easy to use, yet powerful SDK for interfacing from any web site, mobile phone, or desktop application, covering most platforms and development environments. Unfortunately, among the code samples in their web site, there aren't any C++ samples, so I decided to develop my own C++ implementation.

    Sending SMS Messages Programmatically

    Most applications and web sites used to send SMS messages as part of their scope or among other functionalities (i.e., sending alerts, etc.) use one of the following methods:
    • HTTP Web Service - requires using HTTP "GET" method to send a given Web Service a command, using an API, which contains the credentials, parameters, and the text for this message.

    • Email to SMS - uses the SMTP protocol to allow sending an email in a unique format, which encodes all required parameters (credentials, sender, receiver, etc.) as part of an email.
    This article focuses on the first method, using a Web Service.

    The API

    The following table lists all parameters that can (or should) be sent to the Web Service:


    Using the code

    The code in this article was developed in MFC / C++ using Visual Studio 2010 Ultimate. I also used Cheng Shi's HTTPClient (thanks Cheng!).

    In order to use the code for your own application, it is advised to read the specifications for the SDK named HTTPSMS . Secondly, you need to open an account and obtain your user name and password, which can be hardcoded in the source code, or entered during runtime.

    The SendSMS application

    The main functionality of our application is obviously sending an SMS, which is done in the following function:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    // SendSms  - by Michael Haephrati
    BOOL SendSms(CString From, CString CountryCode, CString To,CString Message,CString *Status)
        // From - the ID or number you are sending from. This is what will appear at the recipient's cellphone. 
        // CountyCode - the code of the country you are sending the SMS to (for example: 44 is for the UK
        // To - is the number you are texting to, which should not contain any leading zeros, spaces, commas, etc.
        // Message - is the message you are sending, which can be any multi lingual text
        // The status returned would be either a confirmation number along with the text "OK", which means that the message
        // was delivered, or an error code. 
    {
        BOOL result=FALSE;
        wstring user=L"PLACE_YOUR_USERNAME_HERE",pass=L"PLACE_YOUR_PASSWORD_HERE",request=L"";
        // 
        request=L"http://sms1.cardboardfish.com:9001/HTTPSMS?S=H&UN=";
        request+=user;    // user name
        request+=L"&P=";
        request+=pass;    // password
        request+=L"&DA="; 
        request+=(wstring)(CountryCode+To); // country code
        request+=L"&SA="; 
        request+=(wstring)From; // From (sender ID)
        request+=L"&M=";
        CString EncodedMessage; // Message
        
        CString ccc;
        EncodedMessage=ConvertHex(Message)+ConvertHex( L" here you can place your marketing piech, website, etc.");
        
        request+=(wstring)EncodedMessage; // Message to send
    
        request+=L"&DC=4";
        // Indicating that this message is encoded as opposed to plain text  


    Now we handle the HTTP "GET" request:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    WinHttpClient client(request); 
            
         client.SendHttpRequest(L"GET",true);
        // Get the response
    
        wstring httpResponseHeader = client.GetResponseHeader();
        wstring httpResponseContent = client.GetResponseContent();
        *Status=httpResponseContent.c_str();
        return result; 
    }


    Other Services

    I have tested the services of CardBoardFish, which I used for the attached source code. They provide their own code samples here, but these don't include c++, which I why I wrote the test application attached to this article.

    I recently tested another service they provide which is verifying mobile numbers before sending the text messages. I didn't include this functionality because I found it to be too slow, and also it doesn't cover some countries, among them... USA.

    I found another alternative service provider : http://www.clickatell.coml so there are several options to choose from.

    Further Reading 

    Please refer to another article of mine, this time explaining how to do the same using iOS (iPhone / iPad).


    License


    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    About the Author

    Michael N. Haephrati, is an entrepreneur, inventor and a musician. Haephrati worked on many ventures starting from HarmonySoft, designing Rashumon, the first Graphical Multi-lingual word processor for Amiga computer. During 1995-1996 he worked as a Contractor with Apple at Cupertino. Worked at a research institute made the fist steps developing the credit scoring field in Israel. He founded Target Scoring and developed a credit scoring system named ThiS, based on geographical statistical data, participating VISA CAL, Isracard, Bank Leumi and Bank Discount (Target Scoring, being the VP Business Development of a large Israeli institute).
    During 2000, he founded Target Eye, and developed the first remote PC surveillance and monitoring system, named Target Eye.

    Other ventures included: Data Cleansing (as part of the DataTune  system which was implemented in many organizations.





    Follow on , ,



    Article Top

    Attachments: [Haephrati_SendSMS.zip]