Product
Docs
Resources
Log In

Sign Up

3 Ways To Send Text Messages (SMS) with Java

Traditionally, emails were considered the industry standard for delivering important application notifications. It allowed application users to stay updated with important application events without having to log into it every day. However, emails are generally considered to convey messages of less importance as emails are not checked frequently. Therefore, most people ended up missing out on crucial application notifications.

Hence, developers now focus on utilizing more reachable services to deliver system notifications. One such service is the use of SMS messages. SMS messages are frequently checked and prominent as they appear on your mobile phone. Hence, many mission-critical applications have now adopted the use of SMS to notify their consumers of events, such as - One-Time Password Links, MFA Codes, and even application notifications (Facebook uses SMS to inform users about birthdays).

This article will present three ways developers can implement an SMS-driven notification service using Java.

Sending SMS using Java (Pre-Note)

It is essential to understand that there is no native implementation that Java offers to send SMS notifications. Therefore, integrating third-party services is the only way to send SMS notifications via Java.

Some of these tools are paid, while some offer a free plan. Therefore, you can select a third-party service based on your requirements.

01 - Twilio

Twilio is one of the most commonly used services for SMS messaging across several languages. It offers an API that developers can use to send and receive SMS, MMS, or even WhatsApp messages.

Note that Twilio's SMS pricing will change depending on the country that you are located on, message type, and destination. For example, Twilio offers a pay-as-you-go model in Sri Lanka where all messages sent within Sri Lanka are free and only require a payment per SMS sent to an international number. Its pricing model is included below.

Twilio SMS Pricing

Figure - Twilio Pricing

Pros

  1. Minimal latency: There is little to no latency in sending SMS messages. It is in near real-time, thus ensuring users are notified as soon as possible.
  2. Good documentation: Twilio offers comprehensive developer documentation that provides step-by-step instructions on setting up Twilio to send SMS messages in various languages.

Cons

  1. Too technical: Twilio requires some experience to interact with the service. Hence, non-technical users might need help interacting with the service.
  2. Requires paid plan for custom Twilio Phone Number: The free plan only offers a Twilio Phone Number for a US or Canada number. To obtain custom Twillio Phone Numbers, you must upgrade to a paid plan.

However, if the pros outweigh the cons, refer to the guide below to find out how you can utilize Twilio to send SMS in Java.

Setting up Twilio

To start sending SMS-driven notifications using Twilio, you require a Twilio account. First, visit their sign-up page and create a free account. After creating an account and verifying your email and phone number, you should see the output below.

Tutorial: How to Send SMS Using Twilio API & Java

Twilio SMS Setup

Figure - Confirming email and phone number on Twilio

Hereafter, select "SMS" as the Twilio product, and fill in the remaining questions with your preferred answer. The answers I've provided are illustrated below.Twilio Confirm Email and Phone Number

Figure - Twilio Sign-Up Configuration

Hereafter, select "Get Started with Twillio" which will redirect you to your Twilio dashboard.

Twilio SMS Dashboard Welcome

Figure - Twilio Dashboard

Initially, you will have to create a Twilio Phone Number. A Twilio Phone Number is a virtual phone number used to send and receive SMS. Since we are using the free plan, we are restricted to only a US or Canadian phone number. However, you can upgrade your plan and get a local Twilio phone number.

Click on "Phone Numbers", as shown in the figure above, and select "Buy a Number".

Twilio Buy Phone Number

Figure - Purchasing a number.

Hereafter, select "US" as the country and purchase any number. This will be used as your Twilio Phone Number. Ensure that the selected number has SMS Capabilities.

Hereafter, you can install the Twilio library using Maven with the syntax shown below.

1 2 <dependency><groupId>com.twilio.sdk</groupId><artifactId>twilio</artifactId><version>9.1.0</version> </dependency>

Afterward, you can use the syntax shown below to send the SMS message.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 import com.twilio.Twilio; import com.twilio.rest.api.v2010.account.Message; import com.twilio.type.PhoneNumber; public class TwilioSMS { public static void main(String[] args) { Twilio.init("<<ACCOUNT_SID>>", "<<AUTH_TOKEN>>"); PhoneNumber to = new PhoneNumber("<<RECIPIENT-NUMBER>>"); PhoneNumber from = new PhoneNumber("<<TWILIO-PHONE-NUMBER>>"); String body = "This is a test message!"; Message message = Message.creator(to, from, body).create(); System.out.println(message.getSid()); } }

02 - MessageBird

Secondly, developers can utilize the service - MessageBird to deliver SMS notifications via a Java application. MessageBird is a developer-centric omnichannel communications channel that offers a messaging service billed on a per-message basis that changes based on the country. You can find a complete breakdown of its pricing here.

Pros

  1. Good documentation: MessageBird offers excellent comprehensive developer documentation that provides step-by-step instructions on setting up, to sending SMS messages in various languages.
  2. Offers multiple channels: Users can implement several messaging channels, improving notification scalability.

Cons

  1. Too technical: MessageBird requires some experience to interact with the service. Hence, non-technical users might find it challenging to interact with the service.

If added scalability benefits are a decisive factor for you, refer to the guide below to set up and send SMS messages using MessageBird.

Tutorial: How to Send SMS Using MessageBird & Java

Setting Up

Initially, you will need to create a free account on MessageBird. Next, ensure that you have verified your phone number. This is mandatory as it is required to get your free testing credits to try out MessageBird.

MessageBird Verify Your Number

Figure - Verifying phone number on MessageBird.

After providing the required information, you will be directed to the MessageBird dashboard, as shown below.

MessagedBird Dashboard

Figure - MessageBird Dashboard

Hereafter, navigate to "SMS" -> "API Getting Started" via the URL depicted below.

1 https://dashboard.messagebird.com/en/getting-started/sms

Hereafter, follow the Getting Started guide and provide your phone number to obtain the free credits.

MessageBird SMS Get Started

Figure - Configuring MessageBird

Using MessageBird API in Java

Hereafter, you can add the code required to send the SMS using MessageBird in Java. To do so, first install the MessageBird API using Maven. The Maven Dependency is illustrated below.

1 2 3 4 5 <!-- https://mvnrepository.com/artifact/com.messagebird/messagebird-api --> <dependency><groupId>com.messagebird</groupId> <artifactId>messagebird-api</artifactId> <version>5.1.0</version> </dependency>

Hereafter, you can use the code snippet shown below to send an SMS message using MessageBird.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import com.messagebird.MessageBirdClient; import com.messagebird.MessageBirdService; import com.messagebird.MessageBirdServiceImpl; import com.messagebird.objects.Message; public class MessageBirdSMS { public static void main(String[] args) { MessageBirdService service = new MessageBirdServiceImpl("<<YOUR-TEST-ACCESS-KEY>>"); MessageBirdClient client = new MessageBirdClient(service); String originator = "Test Message"; String body = "This is a test message"; String recipients = "<<YOUR-VERIFIED-NUMBER>>"; Message message = new Message(originator, body, recipients); try { client.sendMessage(message); } catch (Exception ex) { ex.printStackTrace(); } } }

Use the code above and replace it with your Test Access Key and your verified phone number (since it is in the free plan). After executing this code, you will receive an SMS message.

03 - Courier

Finally, developers can utilize Courier - a multi-channel notification service that simplifies product notifications. Unlike the first two, Courier offers a similar API to a wide variety of notification channels such as - Slack, Email, WhatsApp, and even popular SMS services such as Twilio!

Pros

  1. Efficient UI: Its user interface makes it easy for non-technical people to interact with Courier and deliver product notifications.
  2. Excellent documentation: Courier offers good documentation, everything from setting up to delivering product notifications.
  3. Offers analytics: It allows users to track when they viewed and opened the message.

Cons

  1. Can be costly: It can be expensive to consume multiple notification channels.
  2. Enforced notification count: You are bounded by the maximum monthly notification count (based on the provider).

However, if having analytics and a good UI is a decision-maker for you, refer to the guide below to integrate Courier into a Java application to deliver SMS messages.

Tutorial: How to Send SMS Using Courier & Java

Setting up Courier

Initially, navigate to their sign-up page and create a free account. After creating a free account, you will be guided to your account dashboard, as depicted below.

Send SMS using Java Dashboard

Figure - Courier Dashboard

Switch to your Test workspace to proceed. Since Courier is a multi-channel notification service, it acts as a middleware for various notification services. Hence, to implement an SMS notification service using Courier, we must set up integrations with third-party notification services such as Twilio or MessageBird. However, the main advantage of using Courier rather than the standalone service is that Courier offers the same API for all services. Hence it is straightforward to change your notification service in the future.

Configuring Twilio with Courier

To configure Twilio with Courier, head over to "Channels". You will see the output shown below.

Courier Twilio Java Setup

Figure - Viewing channels offered by Courier

Hereafter, navigate to the "SMS" section and:

  1. Click on Twilio
  2. Fill in the information below by referring to your Twilio Account.

Twilio SMS Setup

Figure - Configuring Twilio on Courier

Afterward, click "Install Provider". If the provider is installed correctly, it will appear in the "Configured Providers" list, as shown below.

Configuring Twilio on Courier

Figure - Confirming Twilio Setup

Hereafter, install the Twilio SDK with Maven using the syntax shown below.

1 2 <dependency><groupId>com.courier</groupId><artifactId>courier-java</artifactId><version>1.6.0</version><scope>compile</scope> </dependency>

Afterward, you will need to create an SMS notification using the Template Notification Designer offered by Courier.

Courier Send SMS Channel

Figure - Configuring the SMS Notification Template

Select "Twilio" from the Configured list shown in the figure above. Afterward, you can start designing your SMS message.

SMS Template Editor for Java

Figure - Creating the template

Finally, publish your messaging template and copy the notification ID.Publish SMS TemplateFigure - Copying the notification ID

Hereafter, you can use the syntax shown below to send the SMS message using Courier.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 import models.SendEnhancedRequestBody; import models.SendEnhancedResponseBody; import models.SendRequestMessage; import services.Courier; import services.SendService; import java.io.IOException; import java.util.HashMap; public class CourierSMS { public static void main(String[] args) { Courier.init("<<YOUR_AUTH_TOKEN_HERE>>"); SendEnhancedRequestBody request = new SendEnhancedRequestBody(); SendRequestMessage message = new SendRequestMessage(); HashMap<String, String> to = new HashMap<String, String>(); to.put("phone_number", "<<RECEIVER-PHONE-NUMBER>>"); message.setTo(to); HashMap<String, Object> content = new HashMap<String, Object>(); content.put("title", "Welcome!"); message.setContent(content); HashMap<String, Object> data = new HashMap<String, Object>(); data.put("name", "Peter Parker"); message.setData(data); request.setMessage(message); try { SendEnhancedResponseBody response = new SendService().sendEnhancedMessage(request); System.out.println(response); } catch (IOException e) { e.printStackTrace(); } } }

Conclusion

This article proposed three ways to send SMS notifications via Java.

This article helps you select the best SMS notification service for the following Java application. Doing so will help your Java applications easily send event-driven SMS text messages to your customers, allowing them to stay on top of crucial application events!

Thank you for reading.

View More Guides

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Product

Pricing

Providers

Developers

Documentation

API

Libraries

Status

© 2023 Courier. All rights reserved.