Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

Send an MMS with Twilio using a media URL

See more Twilio Examples

Send an image and an optional caption from an MMS-capable Twilio number. Supply an MMS-supported destination in E.164 format and replace the example media URL with an HTTPS URL Twilio can fetch. The host must handle HEAD and GET and return the correct Content-Type for the file.

Live send. With live credentials and valid inputs, this request submits a billable message. Save the Message SID to check delivery later. Supported media types and size limits.

Twilio Messages API reference

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // Assumes Chilkat is unlocked. See Global Unlock Sample.
        Http http = new Http();
        http.setRequireSslCertVerify(true);
        http.setBasicAuth(true);
        // Load the owning Account SID and an API key from application configuration.
        String accountSid = "YOUR_ACCOUNT_SID";
        http.setLogin("YOUR_API_KEY_SID");
        http.setPassword("YOUR_API_KEY_SECRET");
        StringBuilder url = new StringBuilder();
        url.append("https://api.twilio.com/2010-04-01/Accounts/");
        url.append(accountSid);
        url.append("/Messages.json");

        // Add raw field values. Chilkat form-encodes them, including the + in a phone number.
        HttpRequest req = new HttpRequest();
        req.setHttpVerb("POST");
        req.setContentType("application/x-www-form-urlencoded");
        req.addParam("From", "YOUR_MMS_ENABLED_NUMBER");
        req.addParam("To", "RECIPIENT_E164_NUMBER");
        // Supply a URL for the file itself so Twilio can retrieve the media.
        req.addParam("MediaUrl", "https://media.example.com/photo.jpg");
        req.addParam("Body", "Photo attached.");

        HttpResponse resp = new HttpResponse();
        JsonObject reply = new JsonObject();
        try {
            http.httpReq(url.getAsString(), req, resp);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (resp.getStatusCode() == 0) {
            System.out.println(http.getLastErrorText());
            return;
        }

        boolean parsed;
        try {
            reply.load(resp.getBodyStr());
            parsed = true;
        } catch (ChilkatException e) {
            parsed = false;
        }
        if ((!success) || (resp.getStatusCode() != 201)) {
            System.out.println("HTTP " + resp.getStatusCode());
            if ((parsed) && (reply.hasMember("code"))) {
                System.out.println("Twilio error " + reply.intOf("code") + ": " + reply.stringOf("message"));
            }

            return;
        }

        if (!parsed) {
            System.out.println("Expected a JSON response.");
            return;
        }

        if ((reply.jsonTypeOf("sid") != 1) || (reply.jsonTypeOf("status") != 1)) {
            System.out.println("The create response is missing the message SID or status.");
            return;
        }

        System.out.println("Message SID: " + reply.stringOf("sid"));
        System.out.println("Initial status: " + reply.stringOf("status"));
    }
}