How to send multiple sms from Java

The most simple way to send SMS from Java 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.

how to send multiple sms from java
Figure 1 - How to send multiple SMS from Java

Java code to send multiple sms to mobile

The Java sms code sample below demonstrates how you can send SMS using the http rest sms api of Ozeki SMS Gateawy using the Java 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.

Main.java
import Ozeki.Libs.Rest.*;

public class Main {

    public static void main(String[] args) {

        var configuration = new Configuration();
        configuration.Username = "http_user";
        configuration.Password = "qwe123";
        configuration.ApiURL = "http://127.0.0.1:9509/api";

        var msg1 = new Message();
        msg1.ToAddress = "+36201111111";
        msg1.Text = "Hello world 1";

        var msg2 = new Message();
        msg2.ToAddress = "+36202222222";
        msg2.Text = "Hello world 2";

        var msg3 = new Message();
        msg3.ToAddress = "+36203333333";
        msg3.Text = "Hello world 3";

        var api = new MessageApi(configuration);
        
        var result = api.Send(new Message[] { msg1, msg2, msg3 });
        
        System.out.println(result);
    }
}
	

How to use the Java sms example:

You can use the Message class to create the SMS and 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.

Download SendMultipleSms.java

The source code explained in this article can be downloaded and used and modified free of charge.
Download: SendMultipleSms.java.zip (90.1Kb)

What is in the SendMultipleSms.java.zip file?

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

Figure 2 - SendMultipleSms.java directory

How to send multiple SMS from Java (Quick steps)

To send multiple sms from Java:

  1. Install Ozeki SMS Gateway
  2. Connect Ozeki SMS Gateway to the mobile network
  3. Send a test sms from Ozeki GUI
  4. Create a HTTP sms api user
  5. Apache NetBeans
  6. Create a project called SendMultipleSms
  7. Put the code into a newly created Main.java file or use the Main.java file in the .zip
  8. Create the SMSs by creating multiple new Message objects
  9. Create an api to send your message
  10. Use the Send method to send your messages
  11. Read the response message on the console
  12. Check the logs in the SMS gateway

Install Ozeki SMS Gateway and create an HTTP API user

To be able to send SMS from Java, first you need to install Ozeki SMS Gateway. The SMS gateway can be installed on the same computer, where you develop your Java code in Apache NetBeans. 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 Java code.

HTTP API url to use send sms from Java

To send SMS from Java, your Java 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 Java 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 Java

To authenticate the Java 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 Java you can use the following code to do this encoding:

var usernamePassword = username + ":" + password;
var encodedUsernamePassword = usernamePassword.getBytes();
return "Basic " + Base64.getEncoder().encodeToString(encodedUsernamePassword);
	

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 Java

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 Java

To submit the SMS, your Java 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.

HTTP/1.1 200 OK
User-Agent: OZEKI 10.3.114 (www.myozeki.com)
Content-Type: application/json; charset=utf8
Last-Modified: Fri, 04 Jun 2021 09:45:50 GMT
Server: 10/10.3.114
Transfer-Encoding: chunked

{
  "http_code": 200,
  "response_code": "SUCCESS",
  "response_msg": "Messages queued for delivery.",
  "data": {
    "total_count": 3,
    "success_count": 3,
    "failed_count": 0,
    "messages": [
      {
        "message_id": "d6bac8f4-278d-4de0-bc28-b245c3d63b20",
        "from_station": "%",
        "to_address": "+36201111111",
        "to_station": "%",
        "text": "Hello, World 1",
        "create_date": "2021-06-07 09:05:09",
        "valid_until": "2021-06-14 09:05:09",
        "time_to_send": "2021-06-14 09:05:09",
        "submit_report_requested": true,
        "delivery_report_requested": true,
        "view_report_requested": false,
        "tags": [
          {
            "name": "Type",
            "value": "SMS:TEXT"
          }
        ],
        "status": "SUCCESS"
      },
      {
        "message_id": "5b8bb56c-61fa-44dc-b774-1165c60fdf62",
        "from_station": "%",
        "to_address": "+362222222",
        "to_station": "%",
        "text": "Hello, World 2",
        "create_date": "2021-06-07 09:05:09",
        "valid_until": "2021-06-14 09:05:09",
        "time_to_send": "2021-06-14 09:05:09",
        "submit_report_requested": true,
        "delivery_report_requested": true,
        "view_report_requested": false,
        "tags": [
          {
            "name": "Type",
            "value": "SMS:TEXT"
          }
        ],
        "status": "SUCCESS"
      },
      {
        "message_id": "ea454898-451e-4c64-b8ab-c3d9d137ca93",
        "from_station": "%",
        "to_address": "+363333333",
        "to_station": "%",
        "text": "Hello, World 3",
        "create_date": "2021-06-07 09:05:09",
        "valid_until": "2021-06-14 09:05:09",
        "time_to_send": "2021-06-14 09:05:09",
        "submit_report_requested": true,
        "delivery_report_requested": true,
        "view_report_requested": false,
        "tags": [
          {
            "name": "Type",
            "value": "SMS:TEXT"
          }
        ],
        "status": "SUCCESS"
      }
    ]
  }
}
	

HTTP response received by the Java 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.114 (www.myozeki.com)
Content-Type: application/json; charset=utf8
Last-Modified: Fri, 04 Jun 2021 09:45:50 GMT
Server: 10/10.3.114
Transfer-Encoding: chunked

{
  "http_code": 200,
  "response_code": "SUCCESS",
  "response_msg": "Messages queued for delivery.",
  "data": {
    "total_count": 3,
    "success_count": 3,
    "failed_count": 0,
    "messages": [
      {
        "message_id": "d6bac8f4-278d-4de0-bc28-b245c3d63b20",
        "from_station": "%",
        "to_address": "+36201111111",
        "to_station": "%",
        "text": "Hello, World 1",
        "create_date": "2021-06-07 09:05:09",
        "valid_until": "2021-06-14 09:05:09",
        "time_to_send": "2021-06-14 09:05:09",
        "submit_report_requested": true,
        "delivery_report_requested": true,
        "view_report_requested": false,
        "tags": [
          {
            "name": "Type",
            "value": "SMS:TEXT"
          }
        ],
        "status": "SUCCESS"
      },
      {
        "message_id": "5b8bb56c-61fa-44dc-b774-1165c60fdf62",
        "from_station": "%",
        "to_address": "+362222222",
        "to_station": "%",
        "text": "Hello, World 2",
        "create_date": "2021-06-07 09:05:09",
        "valid_until": "2021-06-14 09:05:09",
        "time_to_send": "2021-06-14 09:05:09",
        "submit_report_requested": true,
        "delivery_report_requested": true,
        "view_report_requested": false,
        "tags": [
          {
            "name": "Type",
            "value": "SMS:TEXT"
          }
        ],
        "status": "SUCCESS"
      },
      {
        "message_id": "ea454898-451e-4c64-b8ab-c3d9d137ca93",
        "from_station": "%",
        "to_address": "+363333333",
        "to_station": "%",
        "text": "Hello, World 3",
        "create_date": "2021-06-07 09:05:09",
        "valid_until": "2021-06-14 09:05:09",
        "time_to_send": "2021-06-14 09:05:09",
        "submit_report_requested": true,
        "delivery_report_requested": true,
        "view_report_requested": false,
        "tags": [
          {
            "name": "Type",
            "value": "SMS:TEXT"
          }
        ],
        "status": "SUCCESS"
      }
    ]
  }
}
	

How to send SMS from Java using the Java sms api (Video tutorial)

This video shows you how to create a new project in Apache NetBeans, how to name it to Send-multiple-SMS. Once the solution is created, you might notice that a there is a folder called <default package> in your project inside the Source Packages folder. This is where you will create or paste the Main.java file. Then you have to paste the Ozeki folder into the Source packages folder and this is all the preparations you need to send an SMS using Java.

Video 1 - How to download Ozeki.Libs.Rest and set up your project

Java sms example: Main.java

Figure 3 - SendMultipleSms project

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.

Video 2 - Sending multiple SMS with the Java code above

Summary

This guide was meant to explain the steps of multiple SMS sending in Java with Ozeki SMS Gateway. If the steps were followed carefully, reaching out to several customers at once should not cause any kind of problem. Multiple SMS sending can be really useful if you or your company want to send out information to a large number of people. Ozeki SMS Gateway offers you the ability to route SMS messages between various endpoints and gives the ability to control SMS messages as they go through the system.

Make sure that the studying is not finished here, visit Ozeki's tutorial page and read about topics like SMS scheduling and deleting in Java.

Now the only thing to do is to download Ozeki SMS Gateway and let the work begin!

More information