Using Twilio an application can communicate with other devices like phone, fax, other web applications. In this blog, we will see how to send a text message on phone with the minimum code.

Step1 — Add library

First, you need to add Twilio library in your application. If you are using Maven then add following code in your pom.xml.

<dependency>
  <groupId>com.twilio.sdk</groupId>
  <artifactId>twilio</artifactId>
  <version>7.11.0</version>
</dependency>

Or if you are using Gradle build tool then add following code in your build.gradle.

compile group: "com.twilio.sdk", name: "twilio", version: "7.11.0"

or

compile 'com.twilio.sdk:twilio:7.11.0'

Or download Twilio jar and add in your application.

Step2 — Send SMS

Now use the following code to send messages.

TwilioRestClient client = new TwilioRestClient.Builder("ACCOUNT_SID", "AUTH_TOKEN").build();
Message msg = new MessageCreator(new PhoneNumber(toPhoneNumber), new PhoneNumber(grailsApplication.config.authy.fromPhoneNumber), message).create(client);

or

Twilio.init("ACCOUNT_SID", "AUTH_TOKEN");
Message msg = Message.creator(new PhoneNumber(toPhoneNumber), new PhoneNumber(grailsApplication.config.authy.fromPhoneNumber), message).create();

That’s it. Enjoy 🙂