Tropo is part of CiscoLearn More

Sending outbound SMS with Java

Posted on May 6, 2011 by mpermar

Tropo’s Java API allows you to send SMS messages from your Java applications with little or no effort. In this blog post I’ll go through a very simple application that sends an SMS message with very few lines of code.

To make it a little bit more exciting I will use Quartz‘s latest version. If you are not familiar with it, Quartz is a task scheduler library that you can use in Java to schedule and execute tasks. It is a very powerful library that has tons of different configuration options and integrates quite well with different application servers. Java veterans know it very well as it has been there almost from the very beginning. As Quartz was releasing a major upgrade this March, I decided to test how it works. I also thought that delivering scheduled messages would be a great real-life example of how Tropo can be useful for you. Imagine for example you can use to do SMS broadcasting, send advertisements, send alerts on health monitoring applications and many more exciting use cases.

So let’s start. The first step is to create your application. If you are familiar with Tropo’s WebApi model and in particular with the Java library ( if not, you can have a look to this introductory screencast ) you may expect that we would create a WebApi based application. But this is not the case. We are going to take advantage of Tropo’s Java API REST implementation to make it super-easy to send SMS messages. What we are going to do is to create a script in Tropo that will send the SMS message, get a session token, and invoke that tropo script from our Java application using the session token. So, let’s go. The first step is to create a Scripting based application:

Next, give it a name, like for example QuartzTest and click on the “use a Hosted File” link and next click on the “Create a new hosted file for this application” option, like is shown in the screenshot below:

Ok, we are almost there. Now you need to enter the script contents. This script will send the actual SMS outbound message. I will use the message function and pass the message text and the phone number as parameters. At the docs section you will find plenty of information about all the functions and cool actions you can execute from Tropo scripts. As you can see in the screenshot below, our script is super simple:

Ok, so once you have created your application the last step you need to do is to get your Session API token. We will use that token from Java to run the script that we just have created. So, to get access to your token you only need to go to your application screen and click on the Voice link (message token is also valid) at the bottom of your application. A new window will pop up showing your token information:

And that is it. Let’s go now to the Java world. I’ve created a very simple Java project that uses Quartz and invokes our new and shiny tropo script. I’ve uploaded this project to Github. You can find the source code on this link. Feel free to browse the project sources. As you will see, we are using Tropo’s Java Webapi binary libraries that you can find here. And there is just only two classes. One is the scheduler that creates a quartz job and sends it (I wont go into this code, as it is just some Quartz specific stuff). And the second class is the actual job that sends the SMS message using Tropo. Lets have a look to that class:

public class LongJob implements Job {

public void execute(JobExecutionContext arg0) throws JobExecutionException {

try {
// This coude launches your Tropo application. You can configure your tropo application to
// do things like sending an SMS, calliing a phone, etc.
String token = "f46f1f14bdd7684d9195ad83e1bbce021d0f024ad5e56e8c99cbd10e9cf3b2b026cb68749b41cb487dd09a5d";
Tropo tropo = new Tropo();
Map<String, String> params = new HashMap<String, String>();
params.put("message","This is an SMS message");
params.put("numberToDial", "+34637710708");
tropo.launchSession(token, params);

} catch (InterruptedException e) {

That is all the code you need to send an SMS message from Java. Isn’t it cool? Lets go through the different parts of that snipped:

  1. First we create a new Tropo instance. The Tropo class drives all the interactions with the Tropo cloud.
  2. Next, we build a set of parameters. If you remember our script, we need to pass the message and the number that we are sending the message to. All the keys that you put on that params map will be accessible from your script.
  3. And finally we launch the session using the launchSession method. That method will use our token to launch remotely the Tropo script that we created previously. The script will fetch the parameters from the map and will send the SMS message.

Feel free to download and run the example. One word of caution though. Tropo supports international SMS delivery which is very cool. But it could happen that your country could have not been enabled yet. So, if you find that the SMS messages aren’t getting into your phone then let us know about it because we would have to enable SMS delivery to that country through one of our multiple carrier agreements.

And that is it. Hope you have fun with Tropo and Java!

3 Responses

  1. Augusto says:

    I created an “Application” and assigned it a phone number. I have an outbound “Messaging” token used in the following code below:

    String token = “…”;
    Tropo tropo = new Tropo();
    Map params = new HashMap();
    params.put(“message”, “My message”);
    params.put(“numberToDial”, “15559995555”); // My cell phone
    TropoLaunchResult result = tropo.launchSession(token, params);

    result.getSuccess() returns true but I don’t receive a message on my cell phone. What I am doing wrong?

  2. Martin Perez says:


    Can you open a ticket in Tropo forums? There, someone from customer support will track your issue. Quite probably your account might need some permission to be set up.

  3. EM O. Kim says:

    Can you make your sms webpage in JSP for my exercise purpose?
    How much should I pay for it?
    It shall be able to run on Eclipse in JSP and Java.
    I stay in Korea

    Thank you.

Leave a Reply