Send an SMS Message with Java
Note:
Before you can get started, you need the following already set up:
- Set all SMS API configuration settings.
- JDK 11 or later and a familiarity with how to create a new Java application.
- Gradle and a familiarity with how use the Gradle build tools.
Learn how to quickly send SMS messages in a Java application with the Sinch API.
Install the Sinch Java library
- If you don't have a java project already, create one:
gradle init
- Select application, and then accept all default values.
- Paste the following code into your
build.gradle
file: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
- Assign the following parameters in
App.java
with your values:
Parameter | Your value |
---|---|
SERVICE_PLAN_ID | The service plan ID found on your Sinch Customer Dashboard. |
TOKEN | The API token found on your Sinch Customer Dashboard. Click Show to reveal your API token. |
SENDER | Any 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. |
RECIPIENTS | The number to which you want to send the SMS message. |
- 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:
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
- Explore the API specification to test more endpoints.