How to receive an SMS in Java
The simplest 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.
Java code to send sms to mobile
The Java sms code sample below demonstrates how you can receive SMS using the http rest sms api of Ozeki SMS Gateway 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.javaimport 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 api = new MessageApi(configuration); var result = api.DownloadIncoming(); System.out.println(result); } }
How to use the Java sms example:
You can use the MessageApi class to receive the SMS from the SMS gateway. The SMS gateway will forward messages to you from the Inbox folder.
Download ReceiveSms.java
The source code explained in this article can be downloaded and used and modified free of charge.
Download: ReceiveSms.java.zip (89.6Kb)
What is in the ReceiveSms.java.zip file?
The ReceiveSms.java.zip file contains the Ozeki library, which gives you all the tools necessary to send, mark, receive, and delete SMS messages. You will also find a Main.java file in the project, which contains the example code to show you how to receive SMS messages. This example code is listed below.
How to receive SMS from Java (Quick steps)
To receive sms from Java:
- Install Ozeki SMS Gateway
- Connect Ozeki SMS Gateway to the mobile network
- Send a test sms from Ozeki GUI
- Create a HTTP sms api user
- Apache NetBeans
- Create a project called ReceiveSms
- Put the code into a newly created Main.java file or use the Main.java file in the .zip
- Create an api to receive your messages
- Use the DownloadIncoming method to Receive your message
- Read the response message on the console
- Check the logs in the SMS gateway
Install Ozeki SMS Gateway and create an HTTP API user
To be able to send, receive and delete 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 receive sms from Java
To receive SMS from Java, your Java 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 receive 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 receive SMS from Java
To delete SMS message, 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 receive SMS from Java
To delete the SMS, your Java application will send an HTTP request similar to the one below. Note, that this request contains a HTTP header part and a http body part. The HTTP body is a JSON encoded data string. It contains the recipient's number and the message's text.
GET /api?action=receivemsg&folder=inbox HTTP/1.1 HTTP2-Settings: AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA Connection: Upgrade, HTTP2-Settings Upgrade: h2c Content-Length: 0 Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw== Host: 127.0.0.1:9509 User-Agent: Java-http-client/16.0.1
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 message's submission.
HTTP/1.1 200 OK User-Agent: OZEKI 10.3.120 (www.myozeki.com) Content-Type: application/json; charset=utf8 Last-Modified: Fri, 11 Jun 2021 13:37:11 GMT Server: 10/10.3.120 Transfer-Encoding: chunked { "http_code": 200, "response_code": "SUCCESS", "response_msg": "", "data": { "folder": "inbox", "limit": "1000", "data": [ { "message_id": "0401f09a-5edb-4728-9bbc-0426fd9bea09", "from_connection": "http_user@localhost", "from_address": "+36201111111", "from_station": "%", "to_connection": "http_user@localhost", "to_address": "http_user", "to_station": "%", "text": "Hello world 1", "create_date": "2021-06-11 14:01:15", "valid_until": "2021-06-18 14:01:15", "time_to_send": "2021-06-11 14:01:15", "submit_report_requested": true, "delivery_report_requested": true, "view_report_requested": true, "tags": [ { "name": "Type", "value": "SMS:TEXT" } ] }, { "message_id": "5302e7df-18c4-4e01-82e7-181967bc8516", "from_connection": "http_user@localhost", "from_address": "+36202222222", "from_station": "%", "to_connection": "http_user@localhost", "to_address": "http_user", "to_station": "%", "text": "Hello world 2", "create_date": "2021-06-11 14:01:15", "valid_until": "2021-06-18 14:01:15", "time_to_send": "2021-06-11 14:01:15", "submit_report_requested": true, "delivery_report_requested": true, "view_report_requested": true, "tags": [ { "name": "Type", "value": "SMS:TEXT" } ] }, { "message_id": "95c60379-e541-4be1-b698-adfde4e425c8", "from_connection": "http_user@localhost", "from_address": "+36203333333", "from_station": "%", "to_connection": "http_user@localhost", "to_address": "http_user", "to_station": "%", "text": "Hello world 3", "create_date": "2021-06-11 14:01:15", "valid_until": "2021-06-18 14:01:15", "time_to_send": "2021-06-11 14:01:15", "submit_report_requested": true, "delivery_report_requested": true, "view_report_requested": true, "tags": [ { "name": "Type", "value": "SMS:TEXT" } ] } ] } }
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 ReceiveSMS. 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 receive a SMS using Java.
Java receive sms example: Main.java
How to check that the SMS has been accepted by the HTTP user
After the request 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 management console. The following video shows you what to look for.
Summary
This article's main goal was to demonstrate SMS receiving in Java with the help of Ozeki SMS Gateway. If the steps were followed carefully, message transferring from the inbox to the user's Java program cannot be an issue anymore. If you want to collect the incoming messages to a Java program, this solution is a perfect way to do it. Ozeki SMS Gateway can be downloaded from Ozeki's website and can be used in a trial period free of charge, so you can make sure that this application is made for you.
Continue the reading on Ozeki's tutorial page, similar guides can be found about topics like SMS scheduling and deleting in Java.
The only thing to do now is to download Ozeki SMS Gateway and let the work begin!
More information