Get Started

SMS and MMS Java Quickstart Tutorial

Sending Text Messages via the REST API

It's really easy to send an outgoing SMS using Twilio. To send an SMS, make an HTTP POST request to the Messages resource.

POST https://api.twilio.com/2010-04-01/Accounts/AC123456abc/Messages

Our twilio-java helper library makes this extremely easy. Open a file called SmsSender.java and add the following lines:

SmsSender.java
// Download the twilio-java library from http://twilio.com/docs/libraries
import java.util.Map;
import java.util.HashMap;

import com.twilio.sdk.resource.instance.Account;
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.MessageFactory;
import com.twilio.sdk.resource.instance.Message;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class SmsSender {

    /* Find your sid and token at twilio.com/user/account */
    public static final String ACCOUNT_SID = "AC123";
    public static final String AUTH_TOKEN = "456bef";

    public static void main(String[] args) throws TwilioRestException {

        TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

        Account account = client.getAccount();

        MessageFactory messageFactory = account.getMessageFactory();
        List<NameValuePair> params = new ArrayList();
        params.add(new BasicNameValuePair("To", "+14159352345")); // Replace with a valid phone number for your account.
        params.add(new BasicNameValuePair("From", "+14158141829")); // Replace with a valid phone number for your account.
        params.add(new BasicNameValuePair("Body", "Where's Wallace?"));
        Message sms = messageFactory.create(params);
    }
}

The From number must be a valid Twilio phone number. The To number can be any outgoing number.

It's also easy to send an outgoing MMS using Twilio. To send an MMS, you also make an HTTP POST request to the Messages resource but this time specify one or more MediaUrl parameters.

MMS messages can only be sent and received by numbers having MMS capability. You can check the capabilities of numbers in the account portal or query the Available Phone Numbers resource to search for Twilio numbers that are MMS enabled.
MmsSender.java
// Download the twilio-java library from http://twilio.com/docs/libraries
import java.util.Map;
import java.util.HashMap;

import com.twilio.sdk.resource.instance.Account;
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.MessageFactory;
import com.twilio.sdk.resource.instance.Message;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class MmsSender {

    /* Find your sid and token at twilio.com/user/account */
    public static final String ACCOUNT_SID = "AC123";
    public static final String AUTH_TOKEN = "456bef";

    public static void main(String[] args) throws TwilioRestException {

        TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

        Account account = client.getAccount();

        MessageFactory messageFactory = account.getMessageFactory();
        List<NameValuePair> params = new ArrayList();
        params.add(new BasicNameValuePair("To", "+14159352345")); // Replace with a valid phone number for your account.
        params.add(new BasicNameValuePair("From", "+14158141829")); // Replace with a valid phone number for your account.
        params.add(new BasicNameValuePair("Body", "Where's Wallace?"));
        params.add(new BasicNameValuePair("MediaUrl", "https://static0.twilio.com/packages/foundation/img/twilio-logo-50x50.png"));
        Message mms = messageFactory.create(params);
    }
}

Note that the Body parameter is optional if you are sending one or more MediaUrls.

For more information about sending SMS and MMS messages, see the REST documentation.


Next: Reply to Incoming SMS »