Example on how to send/receive SMS in Java

On this site you will find examples on how to send and receive SMS in Java programming language. This site was designed and created to help you pick the most suitable method of your preference. This information helps you create the SMS solution in JAVA on the server side. If you would like to use JAVA on mobile phones (e.g. J2ME SMS communication), you will find the document about SMS application port numbers useful.


Java is one of the most popular programming language today. It offers many possibilities to develop applications and to build various solutions, but in order to be able to send and receive SMS messages, its possibilities need to be extended. If you are a Java developer and you would like to communicate with mobile users by sending SMS messages to their mobile phones, you need to find a way to communicate with the mobile network. The best way to do this is to operate your own SMS gateway. After you have setup an SMS gateway (for example the Ozeki NG SMS Gateway) in your network, all you have to do is find an appropriate interface to this gateway. In the following part, you can read about the best interfaces available for SMS messaging.

Sending SMS from Java using a database

If you are looking for a solution which assures accuracy, safety and high efficiency, probably the Java database solution will be your best choice. This solution uses a database server, (such as Oracle, MySQL, Java DB, MS SQL, etc) to send and receive SMS messages. We recommend you to use this solution if your Java application already uses a database for storing information.

how to manage messages from java through sql
Figure 1 - How to send and receive SMS messages from Java through SQL

The diagram above illustrates how this solution works. When you want to send an SMS it should be stored in a database table. The SMS gateway installed in your network will periodically check this database table and if it finds any unsent records, it will submit them and after submission it will update the status field of the record. This way your Java application will know if the message was sent or not. Receiving works a similar way. When an SMS comes in, it is stored in a database table. Your JAVA application can read this database table to find new incoming messages.

Read more: How to send SMS from Java through an SMS gateway using SQL

Sending SMS from JAVA using HTTP

This configuration is beneficial for those Java developers who prefer to use HTTP requests in their systems to submit SMS messages. As Java has built in functionality for HTTP, this is a very simple and efficient solution to post messages to the mobile network. HTTP submissions can go through plain an encrypted (HTTPS) connections, thus this solution not only gives you high performance, but can also be used to achieve greater security. If you operate your own SMS gateway, and you pass your messages to it using HTTP requests, SMS messages can be sent and received swiftly to mobile users (Figure 2).

how to send text messages to cellphones form java
Figure 2 - How to send text messages to cellphones from Java.

If you examine the above figure, you can see that in this configuration, the SMS messages are sent directly to the SMS gateway from the Java program using HTTP requests. If an incoming SMS comes in your Java application can receive it in two ways. It can poll the SMS gateway periodically to check for incoming messages, or if it as able to receive incoming HTTP requests, the SMS gateway can post incoming messages to it through HTTP.

Read more: How to send and receive SMS messages using HTTP request in JAVA

Sending SMS From Java using the Ozeki Java SMS SDK

The best option to send and receive SMS messages form Java is to use the Ozeki Java SMS SDK. This SDK gives you a library that communicates with the SMS Gateway through a TCP/IP socket. It is excellent because it is very fast, efficient and it provides asynchronous event handling to notify you about delivery information and incoming messages.

send or receive sms in java through the ozeki
Figure 3 - Send/receive SMS in Java through the Ozeki Java SDK

Figure 3 gives a little insight on how this SDK works. SMS messages are channeled through a TCP connection directly to the Ozeki NG SMS Gateway. The SMS gateway forwards the messages directly to the mobile network and it uses the established TCP channel to return delivery reports to the Java program. The Java program can work with these reports by implementing the events triggered in the SDK.

Read more: How to send and receive SMS messages using the Ozeki Java SDK.

More information