How to Send SMS in Java Using Plivo’s SMS API

Your company has settled on Plivo to handle its voice and messaging communications, and now it’s your job to start integrating Plivo into your company’s applications. Don’t worry — Plivo has an SDK to help you out. Let’s see how to send and receive messages through Plivo in a Java application.

Install the Plivo SDK

We’ll presume you already have Java installed. The easiest way to install the Plivo SDK is by using IntelliJ IDEA. Install it, create a new project, and choose a dependency manager and Java SE SDK for the new project. Then edit pom.xml, add these lines, and save the file.

<dependency>
<groupId>com.plivo</groupId>
<artifactId>plivo-java</artifactId>
<version>4.14.0</version>
</dependency></pre>

Find your Auth ID and Auth Token

You have to have proper credentials before you can use the Plivo API. We provide an Auth ID and Auth Token in the Account section at the top of your Plivo Console.

Choose a phone number

You need an SMS-enabled Plivo phone number if you want to receive incoming calls. Check the Numbers screen of your Plivo console to see what numbers you have available and which of them support SMS capabilities. You can also buy numbers from the “Buy a Number” screen.

Send an SMS message

Now you’re ready to start. Create a Java class in the project called SendSMS and paste in this code:

import java.io.IOException;
import java.util.Collections;
import com.plivo.api.Plivo;
import com.plivo.api.exceptions.PlivoRestException;
import com.plivo.api.models.message.Message;
import com.plivo.api.models.message.MessageCreateResponse;

class SendSMS
{
public static void main(String [] args) throws IOException, PlivoRestException {
Plivo.init("<auth_id>","<auth_token>");
MessageCreateResponse response = Message.creator("+14151234567",
Collections.singletonList("+14157654321"),
"Hello, this is test message")
.create();
System.out.println(response);
}
}

Receive an SMS message

Of course, sending messages is only half of the equation. Plivo supports receiving SMS text messages in many countries (see our SMS API coverage page, and click on the countries you’re interested in). When someone sends an SMS message to a Plivo phone number, you can receive it on your server by using a Spark web app. Install Spark by editing pom.xml again and adding dependencies for Spark and the Simple Logging Facade for Java ( SLF4J), which you’ll also want:

<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
import static spark.Spark.*;

public class ReceiveSms {
public static void main(String[] args) {
get("/receive_sms", (request, response) -> {
// Sender's phone number
String from_number = request.queryParams("From");
// Receiver's phone number - Plivo number
String to_number = request.queryParams("To");
// The text which was received
String text = request.queryParams("Text");
// Print the message
System.out.println(from_number + " " + to_number + " " + text);
return "Message Received";
});
}
}
./ngrok http [portnum]

Conclusion

And that’s all there is to sending and receiving SMS messages using Plivo’s Java SDK. Don’t use Java? Don’t worry — we have SDKs for PHP, Python, Node.js, Ruby, .NET Core, .NET Framework, and Go. (Sorry, COBOL fans.)

Developer Evangelist