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.

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.

    Double check that the region is correct on your base URL. Learn more about regional options here.

  2. 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

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.