How to send sms from C/C++

The most simple way to send SMS from C/C++ is to use the built in HTTP/Rest SMS api of Ozeki SMS Gateway. When you use this API, you will send SMS messages by issuing a HTTP Post request to the SMS gateway. The HTTP Post request will contain a message formatted in json format. The SMS gateway will send this SMS to the recipient phone, and it will return a HTTP 200 OK response to your request.

C/C++ code to send sms to mobile

The C/C++ sms code sample below demonstrates how you can send SMS using the http rest sms api of Ozeki SMS Gateawy using the C/C++ Ozeki.Libs.Rest library. This library is provided to you free of charge, and you may use it and modify it in any of your projects.

SendSms.cpp
#include <iostream>
#include <string>
#include "Ozeki.Libs.Rest.h"

using namespace std;

int main()
{
	//Function to create unique identifier for each messages
    srand((unsigned)time(0));

	Configuration configuration;
	configuration.Username = "http_user";
	configuration.Password = "qwe123";
	configuration.ApiUrl = "http://127.0.0.1:9509/api";

	Message msg;
	msg.ToAddress = "+36201111111";
	msg.Text = "Hello world!";

	MessageApi api(configuration);

	auto result = api.Send(msg);

	cout << result << endl;

	return 0;
}
	

How to use the C/C++ sms example:

This C/C++ sms example can be used in any C or C++ application. To use it, you must add the Ozeki.Libs.Rest dll as a reference to your project. After the project reference is added, you must put the using Ozeki.Libs.Rest; directive into the header section of your C/C++ source code. This will allow you to use the classes provided by the Ozeki.Libs.Rest library. You can use the Message class to create the SMS. You can use the MessageApi class to send the SMS to the SMS gateway. The SMS gateway will forward your message to the mobile network either through a wireless connection or through the Internet.

how to send sms from ccpp
Figure 1 - How to send SMS from C/C++

Download SendSms.cpp

The source code explained in this article can be downloaded and used and modified free of charge.
Download: SendSms.cpp.zip (2.97Mb)

What is in the SendSms.cpp.zip file?

The SendSms.cpp.zip file contains the Ozeki.Libs.Rest library, which gives you all the tools neccessary to send and receive SMS messages. You will also find the SendSms project in the zip, which contains the example code to show you how to send an SMS. This example code is listed below.

Figure 2 - What is inside SendSms.cpp.zip

How to send SMS from C/C++ (Simple guidelines)

To send SMS from C/C++:

  1. Install a HTTP API user
  2. Enable Log communication events on the Advanced tab
  3. Setup Visual Studio
  4. Download then extract the SendSms.cpp.zip file
  5. Open the sendsms.sln file in Visual Studio
  6. Launch Ozeki SMS Gateway app
  7. Run SendSMS.cpp C++ code in Visual Studio
  8. Check the logs to see if the SMS sent

Install Ozeki SMS Gateway and create an HTTP API user

To be able to send SMS from C/C++, first you need to install Ozeki SMS Gateway. The SMS gateway can be installed on the same computer, where you develop your C/C++ code in Visual studio. After installation, the next step is to connect Ozeki SMS Gateway to the mobile network. You can send a test sms from the Ozeki GUI to verify, that your mobile network connection works. The final step to prepare your environment is to create a HTTP sms api user. Create a user with a username of "http_user", and with a password of "qwe123" to make the example work without modification.

After the environment is setup, you can run your C/C++ code.

HTTP API url to use send sms from C/C++

To send SMS from C/C++, your C/C++ will will have to issue an HTTP request to the SMS gateway. The API url is shown below. Note that the IP address (127.0.0.1) should be replaced to the IP address of your SMS gateway. If Ozeki SMS Gateway is installed on the same computer where the C/C++ sms application is running, this can be 127.0.0.1. If it is installed on a different computer it should be the IP address of that computer.

http://127.0.0.1:9509/api?action=rest
	

HTTP authentication to use send sms from C/C++

To authenticate the C/C++ sms client, you need to send the username and password in a base64 encoded string to the server in a HTTP request. The format used is: base64(username+":"+password). In C/C++ you can use the following code to do this encoding:

string usernamePassword = username + ":" + password;
string usernamePasswordEncoded = base64::encode(usernamePassword);
return "Basic " + usernamePasswordEncoded;
	

For example if you encode the username 'http_user' and the password 'qwe123', you will get the following base64 encoded string: aHR0cF91c2VyOnF3ZTEyMw==. To send

HTTP request header to send SMS from C/C++

To send the SMS messages, you need to include the following lines as headers in the HTTP request. Note that we include a content type and an Authorization header.

Content-Type: application/json
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
	

HTTP request to send SMS from C/C++

To submit the SMS, your C/C++ application will send an HTTP request similar to the one below. Note, that this requst contains a HTTP header part and a http body part. The HTTP body is a JSON encoded data string. It contains the recipient number and the messages text.

POST /api?action=sendmsg HTTP/1.1
Content-Length: 320
Content-Type: application/json
Accept: application/json
Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw==
Host: 127.0.0.1:9509

{
	"messages":	[
		{
			"create_date":	"2021-06-29T07:55:54",
			"message_id":	"hbfc030d-68b7-a1i0-6e96-6jgkqml6rnt9",
			"submit_delivery_requested":	true,
			"submit_report_requested":	true,
			"submit_view_requested":	true,
			"text":	"Hello world!",
			"time_to_send":	"2021-06-29T07:55:54",
			"to_address":	"+36201111111",
			"valid_until":	"2021-07-06T07:55:54"
		}
	]
}
	

HTTP response received by the C/C++ sms example

Once the SMS gateway receives this request, it will generate a HTTP response. The HTTP response will contain a status code, to indicate whether the SMS submit request was successful or not. It will also return a JSON encoded structure to provide you useful details about the messages submission.

HTTP/1.1 200 OK
User-Agent: OZEKI 10.3.123 (www.myozeki.com)
Content-Type: application/json; charset=utf8
Last-Modified: Mon, 28 Jun 2021 16:58:02 GMT
Server: 10/10.3.123
Transfer-Encoding: chunked

{
	"http_code": 200,
	"response_code": "SUCCESS",
	"response_msg": "Messages queued for delivery.",
	"data": {
	  "total_count": 1,
	  "success_count": 1,
	  "failed_count": 0,
	  "messages": [
	    {
	      "message_id": "hbfc030d-68b7-a1i0-6e96-6jgkqml6rnt9",
	      "from_station": "%",
	      "to_address": "+36201111111",
	      "to_station": "%",
	      "text": "Hello world!",
	      "create_date": "2021-06-29 07:55:54",
	      "valid_until": "2021-07-06 07:55:54",
	      "time_to_send": "2021-06-29 07:55:54",
	      "submit_report_requested": true,
	      "delivery_report_requested": false,
	      "view_report_requested": false,
	      "tags": [
	        {
	          "name": "Type",
	          "value": "SMS:TEXT"
	        }
	      ],
	      "status": "SUCCESS"
	    }
	  ]
	}
}
	

Connect your SMS gateway to the mobile network and create an HTTP API user account (Video tutorial)

We assume, you have already installed Ozeki SMS Gateway, and you have connected it to the mobile network. In order to be able to send SMS to a mobile phone from C#, you need to setup an HTTP API user account in Ozeki SMS Gateway.In this video, we are going to show you how to do so. The video will start with adding a new user and will end with the working HTTP API. You will also learn about changing the log level of the API. This 30-second long video is easy to follow and detailed, plus we are using the Ozeki SMS Gateway that offers an intuitive user interface. You will have no problem with following the tutorial.

Video 1 - How to setup an HTTP API user account

How to send SMS from C/C++ using the C/C++ sms api (Video tutorial)

This video shows you how to download the SendSms.cpp.zip file from this page, and how to download it's contents in Visual studio. If watch the video, you will notice, that the contents of the SendSms.cpp zip are placed into the Windows desktop. You will also see, that we double click on the sendsms.sln solution file to open the solution. Visual Studio will show some warnings because the file is coming from the web. Sipmly click OK to avoid these warnings.

Video 2 - How to download and run the example project

C/C++ sms example: SendSms.sln

The example code below is part of the SendSms.sln Visual Studio Solution. A visual studio solution can contain multiple projects and multiple files. In this solution there is only one project: SendSms.vcxproj, and one file: SendSms.cpp.

Figure 2 - SendSms.sln

How to check that the SMS has been accepted by the HTTP user

After the SMS has been submitted, it is a good idea to check your SMS gateway, to see what it has received. You can check the log by opening the HTTP user's details from the Ozeki SMS Gateway managment console. The following video shows you what to look for. The video will start with the opened Ozeki SMS Gateway and will end with the Events menu. You will learn where to click and where to look for the log of the HTTP user. The 34-seconds long video is detailed but very easy to understand. You will not be lost in the user interface, because the Ozeki SMS Gateway offers a very intuitive one.

Video 3 - How to check that the SMS has been accepted by the HTTP user

How to check that the SMS has been sent to the mobile network

The final step in verifying the procedure is to take a look at the logs of the mobile network connection. You might have to turn on logging in the configuration of the connection before you send the message to see the logs. If logging is enabled you will see the phone number and the text of the message you have sent.

Video 4 - How to test if the request was accepted by the SMPP client

Receiving an SMS on Android

In this video, you are going to see the process of receiving an SMS message on Android. The 18 seconds long video will start with an ordinary Android home page and shows you the notification in case of an incoming message. You will also learn how to open the Messages application, where you can get more information about the message and answer it. The process is easy to understand and the video shows it in its entirety.

Video 5 - SMS message received on the mobile phone

To sum it up

This article is written to teach you how to send SMS from C / C++ using the HTTP API user of the Ozeki SMS Gateway. The HTTP user connection in the Ozeki SMS Gateway offers you a very reliable connection and amazing speed to help your business work smoothly. This solution can make your organization more efficient because it allows you to send important SMS messages quickly and easily through your C / C++ application.

There are much more articles in order to help you, so make sure that you continue reading. Move on to the How to send multiple SMS from C / C++ tutorial and learn more.

Start using Ozeki SMS Gateway now! Download it from the Ozeki webpage.

More information