Send an SMS Message with Java

Note:

Before you can get started, you need the following already set up:

Learn how to quickly send SMS messages in a Java application with the Sinch API.

Steps:
  1. Install the Sinch Java library.
  2. Send your first SMS message.

Install the Sinch Java library

  1. If you don't have a java project already, create one:
    Copy
    Copied
    gradle init
  2. Select application, and then accept all default values.
  3. Paste the following code into your build.gradle file:
    Copy
    Copied
    dependencies {
    implementation 'com.sinch:sdk-sms:1.0.5'
    implementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.31'
    }
    
    //Sinch SDK is hosted by maven so make sure you have MavenCentral in your repositories
    repositories {
        mavenCentral()
    }

Send your first SMS message

Open App.java in your favorite editor and copy/paste in this sample. Remember to keep your own package com.

Send an SMS message


// Find your serviceplan_id and Token at https://dashboard.sinch.com/sms/api
// Install the Java helper library at https://developers.sinch.com/docs/sms/sdks/java/
// Find your sinch number at // https://dashboard.sinch.com/numbers/your-numbers/numbers
import com.sinch.xms.*;
import com.sinch.xms.api.*;

public class App {
	private static final String SERVICE_PLAN_ID = "YOUR_servicePlanId";
	private static final String TOKEN = "YOUR_API_token";
	private static ApiConnection conn;

	public static void main(String[] args) {
		String SENDER = ""; //Your sinch number
		String[] RECIPIENTS = { "" }; //your mobile phone number

		ApiConnection conn = ApiConnection
								.builder()
								.servicePlanId(SERVICE_PLAN_ID)
								.token(TOKEN)
								.start();
		MtBatchTextSmsCreate message = SinchSMSApi
										.batchTextSms()
										.sender(SENDER)
										.addRecipient(RECIPIENTS)
										.body("Test message from Sinch.")
										.build();
		try {
			// if there is something wrong with the batch
			// it will be exposed in APIError
			MtBatchTextSmsResult batch = conn.createBatch(message);
			System.out.println(batch.id());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		System.out.println("you sent:" + message.body());
	}
}

Fill in your parameters

  1. Assign the following parameters in App.java with your values:
ParameterYour value
SERVICE_PLAN_IDThe service plan ID found on your Sinch Customer Dashboard.
TOKENThe API token found on your Sinch Customer Dashboard. Click Show to reveal your API token.
SENDERAny number you've assigned to your Sinch account. Find the number on your Sinch Customer Dashboard by clicking the service plan ID link and scrolling to the bottom of the page.
RECIPIENTSThe number to which you want to send the SMS message.
  1. Save the file.

Send your first SMS message

Now that your Java project is ready and the Sinch SDK is installed, send a text message to your mobile phone.

To send the message, run the following code:

Copy
Copied
gradle run

You should receive an SMS message to the number you assigned to your RECIPIENTS parameter.

Next steps

The code used in your Java application sends a POST request to the Sinch API /batches endpoint to send the SMS message. Click here to read more about the batches endpoint.

Additional resources

Was this page helpful?

Send an SMS message


// Find your serviceplan_id and Token at https://dashboard.sinch.com/sms/api
// Install the Java helper library at https://developers.sinch.com/docs/sms/sdks/java/
// Find your sinch number at // https://dashboard.sinch.com/numbers/your-numbers/numbers
import com.sinch.xms.*;
import com.sinch.xms.api.*;

public class App {
	private static final String SERVICE_PLAN_ID = "YOUR_servicePlanId";
	private static final String TOKEN = "YOUR_API_token";
	private static ApiConnection conn;

	public static void main(String[] args) {
		String SENDER = ""; //Your sinch number
		String[] RECIPIENTS = { "" }; //your mobile phone number

		ApiConnection conn = ApiConnection
								.builder()
								.servicePlanId(SERVICE_PLAN_ID)
								.token(TOKEN)
								.start();
		MtBatchTextSmsCreate message = SinchSMSApi
										.batchTextSms()
										.sender(SENDER)
										.addRecipient(RECIPIENTS)
										.body("Test message from Sinch.")
										.build();
		try {
			// if there is something wrong with the batch
			// it will be exposed in APIError
			MtBatchTextSmsResult batch = conn.createBatch(message);
			System.out.println(batch.id());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		System.out.println("you sent:" + message.body());
	}
}