Sending SMS in Java: Overview of API Libraries and Integration Examples
Sending SMS messages remains a popular feature for many modern Java applications: from two-factor authentication and notifications to marketing campaigns. In this article, we will look at the main ways to integrate SMS functionality into Java projects, including using popular cloud APIs (Twilio, Nexmo) and direct interaction with an SMS gateway. You will learn how to connect a library, configure a client, and send your first message.
1. Why use API libraries for sending SMS?
Manual work with the SMPP protocol or direct connection to a GSM modem requires deep knowledge of telecommunications and often leads to reliability issues. Ready-made libraries solve these problems "out of the box":
- Ease of integration — just add a dependency in Maven/Gradle.
- Reliability — providers guarantee delivery and provide monitoring.
- Unicode support — Cyrillic and special characters are handled automatically.
- Delivery statuses — you always know if the message was received.
- Scalability — cloud services handle thousands of requests per second.
2. Twilio: The most popular library for SMS
Twilio is the de facto standard for sending SMS and voice messages. The Java SDK allows you to integrate functionality in minutes.
2.1. Setup
Add the dependency to pom.xml (Maven):
<dependency> <groupId>com.twilio.sdk</groupId> <artifactId>twilio</artifactId> <version>9.10.0</version></dependency>2.2. Sending a simple SMS
import com.twilio.Twilio;import com.twilio.rest.api.v2010.account.Message;import com.twilio.type.PhoneNumber;
public class SmsSender { 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("+79876543210"), // sender number (from Twilio) "Hello! This is a test SMS from a Java application.") .create();
System.out.println("SMS sent. SID: " + message.getSid()); }}Important: numbers must be in international format (E.164).
Cost: Twilio charges for each message (about $0.0079 for Russia). There is a free test balance.
3. Nexmo (Vonage): An alternative with flexible pricing
Nexmo (now part of Vonage) also offers a Java SDK. The library features a simple syntax and good support for Cyrillic.
3.1. Setup
<dependency> <groupId>com.vonage</groupId> <artifactId>server-sdk</artifactId> <version>6.5.0</version></dependency>3.2. Sending example
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 NexmoSms { public static void main(String[] args) { VonageClient client = VonageClient.builder() .apiKey("your_api_key") .apiSecret("your_api_secret") .build();
TextMessage message = new TextMessage( "Vonage APIs", "71234567890", "Test SMS from Nexmo on Java" );
SmsSubmissionResponse response = client.getSmsClient().submitMessage(message); if (response.g