Send an SMS in Java

This tutorial will walk you through building a simple Java SMS project that uses Sinch to send an SMS. You can find the finished project on our GitHub at


If you don’t yet have a Sinch developer account, create one at Use the developer dashboard to create an app (you only need to input an app name), and take note of the app key and secret.

java sms setup

Library for Base64 encoding

You will need to download and add the commons codec library to your Eclipse project to encode your app key and secret. You can do so here. In your project, create a libs folder at the same level as src, and move commons-codec-1.10.jar to this folder. Then, right-click the .jar file and click build path –> add to build path

Send an SMS

Start by creating a new class; I’ll name mine To keep things simple, I put the code to send an SMS in the main function. To make this work for you, substitute a phone number (in E.164 format) to which to send the message, your app key, and your app secret.

import org.apache.commons.codec.binary.Base64;

public class Sms {

    public static void main(String[] args) {

        try {
            String phoneNumber = "phone-number";
            String appKey = "your-app-key";
            String appSecret = "your-app-secret";
            String message = "Hello, world!";

            URL url = new URL("" + phoneNumber);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Content-Type", "application/json");

            String userCredentials = "application\\" + appKey + ":" + appSecret;
            byte[] encoded = Base64.encodeBase64(userCredentials.getBytes());
            String basicAuth = "Basic " + new String(encoded);
            connection.setRequestProperty("Authorization", basicAuth);

            String postData = "{\"Message\":\"" + message + "\"}";
            OutputStream os = connection.getOutputStream();

            StringBuilder response = new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            String line;
            while ( (line = br.readLine()) != null)



        } catch (IOException e) {



Now run the project and you will see “Hello, world!” appear as an SMS to the phone number you entered. That’s how easy is it to send Java SMS with Sinch.

Note about sending quotes in your message: If you want to send a message like Jordan says “hi,” you will need to put three backslashes before the quotes that you want to show up in the message.

In addition, you can find SMS prices by country here.