Menu

Expand
Rate 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. The code snippets in this guide target Java SDK 8 or higher, and make use of the Twilio Java Helper Library.

While you can send text-only SMS messages almost anywhere on the planet, sending media is currently only available in the US and Canada. Learn more in this support article.

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

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.

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 for details.

Before you can send messages, you’ll need to sign up for a Twilio account and purchase a Twilio phone number.

If you’re brand new to Twilio, you can sign up for a free trial account to get started. Once you've signed up, head over to your Console 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

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, 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.

If you’re using a trial account, you will need to verify your personal phone number via the console 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

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).

Loading Code Sample...
        
        

        Send an SMS using the Programmable Messaging API

        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. See our guide on how to Send Bulk SMS Messages for more tips.

        Send a Message in Java with Media (MMS)

        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.

        While you can send text-only SMS messages almost anywhere on the planet, sending media is currently only available in the US and Canada.

        Loading Code Sample...
              
              

              Send a Message with an Image URL

              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:

              "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:

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

              What's next?

              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.

              Jeff Linwood Maylon Pedroso Jarod Reyes Ricky Robinett David Prothero Kat King Mica Swyers Elmer Thomas Paul Kamp Stephanie Marchante Carolina Lopez Brianna DelValle Gyasi Calhoun Mathew Roberts Thomas Wanzek
              Rate this page:

              Need some help?

              We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

              Loading Code Sample...
                    
                    
                    

                    Thank you for your feedback!

                    Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

                    Sending your feedback...
                    🎉 Thank you for your feedback!
                    Something went wrong. Please try again.

                    Thanks for your feedback!

                    thanks-feedback-gif