Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Send SMS and MMS Messages in Java


In this tutorial, we'll show you how to send SMS and MMS messages from Java using Twilio's Programmable Messaging(link takes you to an external page). The code snippets in this guide target Java SDK 8 or higher, and make use of the Twilio Java Helper Library(link takes you to an external page).

(warning)

Warning

While you can send text-only SMS messages almost anywhere on the planet(link takes you to an external page), sending media is currently only available in the US and Canada. Learn more in this support article(link takes you to an external page).


Sign up for (or log in to) your Twilio account

sign-up-for-or-log-in-to-your-twilio-account page anchor
(information)

Info

If you have a Twilio account and Twilio phone number with SMS (and MMS if possible) capabilities, you're all set! Feel free to jump straight to the code.

(warning)

Warning

If you are sending SMS to the U.S. or Canada, before proceeding further please be aware of updated restrictions on the use of Toll-Free numbers for messaging, including TF numbers obtained through Free Trial. Please click here(link takes you to an external page) for details.

Before you can send messages, you'll need to sign up for a Twilio account(link takes you to an external page) and purchase a Twilio phone number(link takes you to an external page).

If you're brand new to Twilio, you can sign up for a free trial account(link takes you to an external page) to get started. Once you've signed up, head over to your Console(link takes you to an external page) and grab your Account SID and your Auth Token. You will need those values for the code samples below.


Get a phone number with SMS (and MMS) capabilities

get-a-phone-number-with-sms-and-mms-capabilities page anchor

Sending messages requires a Twilio phone number with SMS capabilities. If you don't currently own a Twilio phone number with SMS capabilities, you'll need to buy one. After navigating to the Buy a Number page(link takes you to an external page), check the 'SMS' box and click 'Search':

Buy A Number.

If you live in the US or Canada and also wish to send MMS messages, you can select the 'MMS' box. When viewing the search results, you can see the capability icons in the list of available numbers:

Click Buy Button.

Find a number you like and click "Buy" to add it to your account.

(warning)

Warning

If you're using a trial account, you will need to verify your personal phone number via the console(link takes you to an external page) so that you can test sending SMSes to yourself.

Learn more about how to work with your free trial account.


Send an SMS Message via the REST API with the Java Helper Library

send-an-sms-message-via-the-rest-api-with-the-java-helper-library page anchor

Sending an outgoing SMS message requires sending an HTTP POST to the Messages resource URI. Using the helper library, you can create a new instance of the Message resource and specify the To, From, and Body parameters for your message.

The first phone number in the example below is the To parameter (your Twilio number), and the second phone number is the From parameter (your mobile number).

Send an SMS using the Programmable Messaging API

send-an-sms-using-the-programmable-messaging-api page anchor
Java

_23
// Install the Java helper library from twilio.com/docs/java/install
_23
_23
import com.twilio.Twilio;
_23
import com.twilio.rest.api.v2010.account.Message;
_23
import com.twilio.type.PhoneNumber;
_23
_23
public class Example {
_23
// Find your Account SID and Auth Token at twilio.com/console
_23
// and set the environment variables. See http://twil.io/secure
_23
public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
_23
public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
_23
_23
public static void main(String[] args) {
_23
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
_23
Message message = Message.creator(
_23
new com.twilio.type.PhoneNumber("+15558675310"),
_23
new com.twilio.type.PhoneNumber("+15017122661"),
_23
"This is the ship that made the Kessel Run in fourteen parsecs?")
_23
.create();
_23
_23
System.out.println(message.getSid());
_23
}
_23
}

Output

_24
{
_24
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"api_version": "2010-04-01",
_24
"body": "This is the ship that made the Kessel Run in fourteen parsecs?",
_24
"date_created": "Thu, 30 Jul 2015 20:12:31 +0000",
_24
"date_sent": "Thu, 30 Jul 2015 20:12:33 +0000",
_24
"date_updated": "Thu, 30 Jul 2015 20:12:33 +0000",
_24
"direction": "outbound-api",
_24
"error_code": null,
_24
"error_message": null,
_24
"from": "+15017122661",
_24
"messaging_service_sid": null,
_24
"num_media": "0",
_24
"num_segments": "1",
_24
"price": null,
_24
"price_unit": null,
_24
"sid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"status": "queued",
_24
"subresource_uris": {
_24
"media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json"
_24
},
_24
"to": "+15558675310",
_24
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json"
_24
}

If you want to send a message to several recipients, you could create an array of recipients and iterate through each phone number in that array.

You can send as many messages as you like, as fast as you like and Twilio will queue them up for delivery at your prescribed rate limit(link takes you to an external page). See our guide on how to Send Bulk SMS Messages for more tips.


Send a Message in Java with Media (MMS)

send-a-message-in-java-with-media-mms page anchor

You may wish to send outgoing MMS using Twilio. To send an MMS, you also make an HTTP POST request to the Messages resource but this time specify a parameter for the URL of media, such as an image.

We'll add a line to our code from the above example that sets the MediaUrl for the message.

Send a Message with an Image URL

send-a-message-with-an-image-url page anchor
Java

_28
// Install the Java helper library from twilio.com/docs/java/install
_28
_28
import com.twilio.Twilio;
_28
import com.twilio.rest.api.v2010.account.Message;
_28
import com.twilio.type.PhoneNumber;
_28
_28
import java.net.URI;
_28
import java.util.Arrays;
_28
_28
public class Example {
_28
// Find your Account SID and Auth Token at twilio.com/console
_28
// and set the environment variables. See http://twil.io/secure
_28
public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
_28
public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
_28
_28
public static void main(String[] args) {
_28
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
_28
Message message = Message.creator(
_28
new com.twilio.type.PhoneNumber("+15558675310"),
_28
new com.twilio.type.PhoneNumber("+15017122661"),
_28
"This is the ship that made the Kessel Run in fourteen parsecs?")
_28
.setMediaUrl(
_28
Arrays.asList(URI.create("https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg")))
_28
.create();
_28
_28
System.out.println(message.getSid());
_28
}
_28
}

Output

_24
{
_24
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"api_version": "2010-04-01",
_24
"body": "This is the ship that made the Kessel Run in fourteen parsecs?",
_24
"date_created": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"date_sent": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"date_updated": "Thu, 24 Aug 2023 05:01:45 +0000",
_24
"direction": "outbound-api",
_24
"error_code": null,
_24
"error_message": null,
_24
"from": "+15017122661",
_24
"num_media": "0",
_24
"num_segments": "1",
_24
"price": null,
_24
"price_unit": null,
_24
"messaging_service_sid": "MGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"sid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"status": "queued",
_24
"subresource_uris": {
_24
"media": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Media.json"
_24
},
_24
"to": "+15558675310",
_24
"uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json"
_24
}

The setMediaUrl() method call in this code tells Twilio where to go to get the media we want to include. This must be a publicly accessible URL - Twilio will not be able to reach any URLs that are hidden or that require authentication.

Just as when you send a simple SMS, Twilio will send data about the message in its response to your request. The JSON response will contain the unique SID and URI for your media resource:


_10
"subresource_uris": {"media": "/2010-04 01/Accounts/ACxxxxxxxx/Messages/SMxxxxxxxxxxxxx/Media.json"}

When the Twilio REST API creates your new Message resource, it will save the image found at the specified media url as a Media resource. Once created, you can access this resource at any time via the API.

You can print this value from your Java code to see where the image is stored. Add the following line to the end of your Example.java file to see your newly provisioned Media URI:


_10
System.out.println(message.getSubresourceUris().get("media"));


Want to build more messaging functionality into your Java Application?

Check out these in-depth resources to take your programmatic messaging a step further:

Also, try our Java SMS Quickstart, and see how to receive and reply to messages in Java.


Rate this page: