Click here to Skip to main content
11,488,877 members (79,400 online)
Click here to Skip to main content

How to send an SMS message from an application

, 1 Jul 2013 CPOL 285.6K 12.3K 224
Rate this:
Please Sign up or sign in to vote.
How to send an SMS from a Desktop Applicaiton (written in C++).

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  

SendSMS Screen

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

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

     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.com 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)

Share

About the Author

Michael N Haephrati (האפרתי)
CEO Secured Globe, Inc.
United States United States
Michael 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   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMessage Automatically Removed Pin
8-Dec-14 0:33
member.:floyd:.8-Dec-14 0:33 
GeneralMessage Automatically Removed Pin
7-Dec-14 14:12
member.:floyd:.7-Dec-14 14:12 
GeneralMessage Automatically Removed Pin
29-Mar-14 15:42
member.:floyd:.29-Mar-14 15:42 
QuestionSend SMS from PC Pin
Daniel Rozsar5-Oct-13 10:15
memberDaniel Rozsar5-Oct-13 10:15 
AnswerRe: Send SMS from PC Pin
muazam124-Dec-14 5:29
groupmuazam124-Dec-14 5:29 
GeneralRe: Send SMS from PC Pin
Michael N. Haephrati4-Dec-14 8:20
professionalMichael N. Haephrati4-Dec-14 8:20 
QuestionVS 2008 and Character set Pin
Sasa Kajic15-Jul-13 1:09
memberSasa Kajic15-Jul-13 1:09 
GeneralMessage Automatically Removed Pin
7-Jul-13 22:56
membercerkeskizi7-Jul-13 22:56 
NewsUpdate Pin
Michael Haephrati1-Jul-13 7:06
mvpMichael Haephrati1-Jul-13 7:06 
QuestionConvertHex Pin
Graeme M Smith30-Jun-13 17:53
memberGraeme M Smith30-Jun-13 17:53 
AnswerRe: ConvertHex [modified] Pin
Michael Haephrati1-Jul-13 5:42
mvpMichael Haephrati1-Jul-13 5:42 
GeneralRe: ConvertHex Pin
Graeme M Smith6-Jul-13 5:18
memberGraeme M Smith6-Jul-13 5:18 
AnswerRe: ConvertHex Pin
Dusan Paulovic29-Sep-13 10:30
memberDusan Paulovic29-Sep-13 10:30 
GeneralRe: ConvertHex Pin
Michael Haephrati29-Sep-13 10:51
mvpMichael Haephrati29-Sep-13 10:51 
GeneralMy vote of 5 Pin
alonbarak14-Jun-13 12:07
groupalonbarak14-Jun-13 12:07 
GeneralMy vote of 5 Pin
Member 432084428-Feb-13 10:38
memberMember 432084428-Feb-13 10:38 
GeneralRe: My vote of 5 Pin
Michael Haephrati1-Jul-13 6:56
mvpMichael Haephrati1-Jul-13 6:56 
GeneralMy vote of 5 Pin
anup69022-Feb-13 7:59
memberanup69022-Feb-13 7:59 
GeneralRe: My vote of 5 Pin
Michael Haephrati22-Feb-13 8:02
mvpMichael Haephrati22-Feb-13 8:02 
GeneralRe: My vote of 5 Pin
anup69022-Feb-13 8:06
memberanup69022-Feb-13 8:06 
QuestionVB version Pin
leoerazo22-Feb-13 7:55
memberleoerazo22-Feb-13 7:55 
AnswerRe: VB version Pin
Michael Haephrati22-Feb-13 8:00
mvpMichael Haephrati22-Feb-13 8:00 
Yes. Try this Visual Basic.NET example which will show you how to send an SMS to the recipient +447000000001, with sender name set to James and message Hello

'First include the HTTPSMS dynamic link library-

Imports HTTPSMS
 
Module SendTextMessage
 
Sub Main()
 
    'Build a try/catch tree to catch any errors

    Try
 
        'Next, set our destination telephone number:

        Dim destination() As String = {"447000000001"}
 
        'And our source (who the recipient will see as 'sender'):

        Dim source As String = "James"
 
        'Create a new SMS object to hold our message

        Dim ourSMS As New SMS
 
        'Populate this SMS object with our message information, including destination, source, message etc.

        ourSMS.SMS(destination, source, Nothing, "Hello", "0", "1", Nothing, "MSG_1", Nothing, Nothing, Nothing)
 
        'Create a new SendSMS object

        Dim ourSendSMS As New SendSMS
 
        'Now set our username and password:

        Dim username As String = "user01"
 
        Dim password As String = "pass01"
 
        'Get ourSendSMS ready by initialising it with our username and password:

        ourSendSMS.initialise(username, password)
 
        'Now send ourSMS using the ourSendSMS object, capturing the responses with an array 'responses()'

        Dim responses() As Integer = ourSendSMS.SendSMS(ourSMS)
 
        'Let the user know we are about to print the server responses:

        Console.WriteLine("Console responses: ")
 
        Dim i As Integer = 0
 
        'Now for all our server responses, print them to screen:

        While i < responses.Length
 
            Console.WriteLine(responses(i))
 
            i = i + 1
 
        End While
 
        Console.ReadLine()
 
        'Catch any errors, and handle them by printing their output to screen

        Catch sendSMSEx As HTTPSMS.SMSClientException
 
        'Print the error message

        Console.WriteLine(sendSMSEx.errMessage)
 
        Return
 
    End Try
 
End Sub
 
End Module


GeneralRe: VB version Pin
leoerazo22-Feb-13 10:11
memberleoerazo22-Feb-13 10:11 
GeneralRe: VB version Pin
Michael Haephrati22-Feb-13 10:13
mvpMichael Haephrati22-Feb-13 10:13 
QuestionSimple but useful info, Pin
Manish K. Agarwal21-Feb-13 23:25
memberManish K. Agarwal21-Feb-13 23:25 
QuestionI really like Pin
Duane Fahmy21-Feb-13 21:14
memberDuane Fahmy21-Feb-13 21:14 
GeneralMy vote of 5 Pin
Member 416352421-Feb-13 6:32
memberMember 416352421-Feb-13 6:32 
GeneralMy vote of 5 Pin
Wen Hao18-Feb-13 15:34
memberWen Hao18-Feb-13 15:34 
GeneralMy vote of 5 Pin
Daniel Cruz18-Feb-13 8:45
memberDaniel Cruz18-Feb-13 8:45 
QuestionHow to send sms from an application. Pin
daviegy18-Feb-13 3:56
memberdaviegy18-Feb-13 3:56 
AnswerRe: How to send sms from an application. Pin
Michael Haephrati מיכאל האפרתי18-Feb-13 4:55
mvpMichael Haephrati מיכאל האפרתי18-Feb-13 4:55 
GeneralRe: How to send sms from an application. Pin
daviegy19-Feb-13 4:55
memberdaviegy19-Feb-13 4:55 
GeneralExcellent Article Pin
Bhasker Kandpal12-Feb-13 9:10
memberBhasker Kandpal12-Feb-13 9:10 
GeneralMy vote of 5 Pin
liliflower35525-Jan-13 2:15
memberliliflower35525-Jan-13 2:15 
GeneralMy vote of 5 Pin
resi243125-Jan-13 1:13
memberresi243125-Jan-13 1:13 
GeneralMy vote of 5 Pin
midulm25-Jan-13 0:08
membermidulm25-Jan-13 0:08 
GeneralMy vote of 5 Pin
balam198824-Jan-13 23:22
groupbalam198824-Jan-13 23:22 
GeneralMy vote of 5 Pin
evan89724-Jan-13 22:43
groupevan89724-Jan-13 22:43 
GeneralMy vote of 5 Pin
Rutuanie24-Jan-13 20:27
memberRutuanie24-Jan-13 20:27 
AnswerSending SMS messages Pin
Michael Haephrati מיכאל האפרתי24-Jan-13 9:39
mvpMichael Haephrati מיכאל האפרתי24-Jan-13 9:39 
GeneralRe: Sending SMS messages Pin
curiousharry10-Feb-13 21:15
membercuriousharry10-Feb-13 21:15 
GeneralRe: Sending SMS messages Pin
curiousharry10-Feb-13 22:31
membercuriousharry10-Feb-13 22:31 
GeneralRe: Sending SMS messages Pin
Michael Haephrati מיכאל האפרתי11-Feb-13 2:01
mvpMichael Haephrati מיכאל האפרתי11-Feb-13 2:01 
GeneralRe: Sending SMS messages Pin
PJ Arends19-Feb-13 11:21
memberPJ Arends19-Feb-13 11:21 
GeneralRe: Sending SMS messages Pin
Michael Haephrati מיכאל האפרתי19-Feb-13 11:30
mvpMichael Haephrati מיכאל האפרתי19-Feb-13 11:30 
GeneralRe: Sending SMS messages Pin
PJ Arends19-Feb-13 11:33
memberPJ Arends19-Feb-13 11:33 
GeneralMy vote of 4 Pin
VitorHugoGarcia24-Jan-13 7:44
memberVitorHugoGarcia24-Jan-13 7:44 
QuestionNeeds formatting Pin
Richard MacCutchan24-Jan-13 2:42
mvpRichard MacCutchan24-Jan-13 2:42 
AnswerRe: Needs formatting Pin
Michael Haephrati מיכאל האפרתי24-Jan-13 9:01
mvpMichael Haephrati מיכאל האפרתי24-Jan-13 9:01 
GeneralRe: Needs formatting Pin
Richard MacCutchan21-Feb-13 3:16
mvpRichard MacCutchan21-Feb-13 3:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 1 Jul 2013
Article Copyright 2012 by Michael N Haephrati (האפרתי)
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid