Sending SMS in Java: How to Use API Libraries for Message Distribution
Sending SMS messages is a popular feature in modern web applications, authorization systems (2FA), notifications, and marketing campaigns. In Java, specialized libraries are used for this purpose, which interact with cloud provider APIs. In this article, we will look at two of the most popular libraries: Twilio and Vonage (formerly Nexmo). You will learn how to set them up, send your first SMS, and handle possible errors.
1. Why Do You Need a Separate Library for SMS?
Working with SMS directly via HTTP requests is possible but inconvenient. Libraries provide:
- Ready-made classes for creating and sending messages.
- Error handling and retry logic for network failures.
- Secure handling of API keys and tokens.
- Support for additional features (delivery status, receiving incoming SMS).
2. Twilio Library for Java
Twilio is one of the market leaders. Its Java SDK (Software Development Kit) is easy to learn and has excellent documentation.
2.1 Adding the Dependency (Maven)
Add the following block to your pom.xml:
<dependency> <groupId>com.twilio.sdk</groupId> <artifactId>twilio</artifactId> <version>9.14.1</version> <!-- Check for the latest version --></dependency>2.2 Sending SMS via Twilio
Example code for sending a message. Make sure you have your Account SID and Auth Token from the Twilio console, as well as a purchased phone number.
import com.twilio.Twilio;import com.twilio.rest.api.v2010.account.Message;import com.twilio.type.PhoneNumber;
public class SmsSender { // Set your credentials public static final String ACCOUNT_SID = "YOUR_ACCOUNT_SID"; public static final String AUTH_TOKEN = "YOUR_AUTH_TOKEN";
public static void main(String[] args) { Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Message message = Message.creator( new PhoneNumber("+71234567890"), // Recipient number new PhoneNumber("+19876543210"), // Your Twilio number "Hello from Java and Twilio!" ).create();
System.out.println("SMS sent. SID: " + message.getSid()); }}Important: Phone numbers must be in international format (starting with +).
3. Vonage (Nexmo) Library for Java
The Vonage SMS API is another popular choice. Its library is called nexmo-java or vonage-java-sdk.
3.1 Adding the Dependency (Gradle)
implementation 'com.vonage:client:7.3.0'For Maven:
<dependency> <groupId>com.vonage</groupId> <artifactId>client</artifactId> <version>7.3.0</version></dependency>3.2 Sending SMS via Vonage
import com.vonage.client.VonageClient;import com.vonage.client.sms.MessageStatus;import com.vonage.client.sms.SmsSubmissionResponse;import com.vonage.client.sms.messages.TextMessage;
public class VonageSms { public static void main(String[] args) { VonageClient client = VonageClient.builder() .apiKey("YOUR_API_KEY") .apiSecret("YOUR_API_SECRET") .build();
TextMessage message = new TextMessage( "VonageAPI", // Sender (up to 11 characters) "+71234567890", // Recipient "Test message from Java and Vonage!" );
SmsSubmissionResponse response = clien Also in library
C++ Benchmarking Guide: Google Benchmark Tutorial & Examples
C++ Standard Library: Complete Overview of Features
Caching in Go (Golang): Redis, in-memory and other solutions
GraphQL Java: A Powerful Tool for Building Modern APIs in Java