Click here to Skip to main content
Click here to Skip to main content

How to send an SMS message from an application

By , 21 Feb 2013
 

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; 
} 

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 Haephrati, born in 1964, 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.
 
Worked with Amdocs and managed several software projects, among them one for the Ministry of Tourism in New Zealand.  During 1995-1996 he worked as a Contractor with Apple at Cupertino. After returning to Israel, worked as a Project Manager with Top Image Systems (mostly with JCC, Nicosia), and then 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+

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5groupalonbarak14-Jun-13 11:07 
Great Article
GeneralMy vote of 5memberMember 432084428-Feb-13 9:38 
You deserve 5 points, But It would be nice to give another example which is independent on paid Services if possible.
Or to define a free Test Server Service if possible to test and debug such source code.
Thanks.
GeneralMy vote of 5memberanup69022-Feb-13 6:59 
can u provide me an executable file, please
thank you..
GeneralRe: My vote of 5mvpMichael Haephrati22-Feb-13 7:02 
That is not possible and is explained in the article (the source code is missing the username and password to be used). The article code uses a service called HTTPSMS which is a paid service.
You can open an account with them. For further details look here:
http://www.cardboardfish.com/products/business/httpsms_xmlsms.html[^]
GeneralRe: My vote of 5memberanup69022-Feb-13 7:06 
thank you for your valuable guidance
QuestionVB versionmemberleoerazo22-Feb-13 6:55 
Mr. Haephrati, it's my pleasure writing this words to you.
I just wanted to know whether you have a visual basic version for this code. I like this world of programming, but I haven't had the training or the time for learning another language.
 
Thanks in advance
 
God bless you!
AnswerRe: VB versionmvpMichael Haephrati22-Feb-13 7: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 versionmemberleoerazo22-Feb-13 9:11 
Thanks for your reply.
I have to replace "user01" an "pass01" for actual values, right? the console shows message error telling that (User or password incorrect)
GeneralRe: VB versionmvpMichael Haephrati22-Feb-13 9:13 
Correct
QuestionSimple but useful info,memberManish K. Agarwal21-Feb-13 22:25 
My vote 5
Manish Agarwal
manish.k.agarwal @ gmail DOT com

QuestionI really likememberDuane Fahmy21-Feb-13 20:14 
I really like your blog and i really appreciate the excellent quality content you are posting here for free for your online readers.
 
Honda used motorcycles
GeneralMy vote of 5memberMember 416352421-Feb-13 5:32 
Very useful
GeneralMy vote of 5memberWen Hao18-Feb-13 14:34 
Nice article, is the sms service is foc?
GeneralMy vote of 5memberDaniel Cruz18-Feb-13 7:45 
Very good article and is an option to send sms from an application.
Although the reality is that you send an email with a specific format to a company that is responsible for sending your sms. Personally, I would like to send from my own application but using a GSM modem or better with my cell phone as a GSM modem using the COM port.
I tried but I could only connect to send.
And I can not receive.
If someone has an idea that contribute.
thanks
 
translated by Google
QuestionHow to send sms from an application.memberdaviegy18-Feb-13 2:56 
The article is really nice but i don't know how to code much using c++, pls i' ll like to know how some1 can develope this same application using c#..... thanks in advance.Thumbs Up | :thumbsup:
AnswerRe: How to send sms from an application.mvpMichael Haephrati מיכאל האפרתי18-Feb-13 3:55 
I have written this article because there wasn't such example in C++. However, Cardboardfish.com website do have c# code samples.
GeneralRe: How to send sms from an application.memberdaviegy19-Feb-13 3:55 
i went to cardboardfish.com but couldn't find anything relating to c# code samples for the application. thanks for your mail...Thumbs Up | :thumbsup:
GeneralExcellent ArticlememberBhasker Kandpal12-Feb-13 8:10 
Great Work!! 5 Votes
Bhasker Kandpal
Analyst

GeneralMy vote of 5memberliliflower35525-Jan-13 1:15 
Very significant article !!
GeneralMy vote of 5memberresi243125-Jan-13 0:13 
Very useful Article
GeneralMy vote of 5membermidulm24-Jan-13 23:08 
Great article !!!
GeneralMy vote of 5groupbalam198824-Jan-13 22:22 
Good job
GeneralMy vote of 5groupevan89724-Jan-13 21:43 
Nice Article
GeneralMy vote of 5memberRutuanie24-Jan-13 19:27 
Great work
AnswerSending SMS messagesmvpMichael Haephrati מיכאל האפרתי24-Jan-13 8:39 
Some readers seem to be a bit disappointed from the need to interact with the 3rd party service provider to send an SMS. It would have been great to copy and paste some lines of code, and have an SMS sent without using any service, but that's not possible. First, you need to interact with a mobile phone company. The service provider in my code sample, is the front end between the API and around 130 mobile carriers all over the world. Each carrier has its own table of rates and they delivery SMS messages for a small fee, as nothing is free... So in my opinion, using the HTTPSMS protocol seems to be a sufficient choice for our purpose.

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130619.1 | Last Updated 21 Feb 2013
Article Copyright 2012 by Michael Haephrati
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid