Sample code for 30+ languages & platforms
CkJava 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 CkJava Downloads

CkJava
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

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

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkHttp http = new CkHttp();
    http.put_RequireSslCertVerify(true);
    http.put_BasicAuth(true);
    //  Load the owning Account SID and an API key from application configuration.
    String accountSid = "YOUR_ACCOUNT_SID";
    http.put_Login("YOUR_API_KEY_SID");
    http.put_Password("YOUR_API_KEY_SECRET");
    CkStringBuilder url = new CkStringBuilder();
    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.
    CkHttpRequest req = new CkHttpRequest();
    req.put_HttpVerb("POST");
    req.put_ContentType("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.");

    CkHttpResponse resp = new CkHttpResponse();
    CkJsonObject reply = new CkJsonObject();
    success = http.HttpReq(url.getAsString(),req,resp);
    if (resp.get_StatusCode() == 0) {
        System.out.println(http.lastErrorText());
        return;
        }

    boolean parsed = reply.Load(resp.bodyStr());
    if ((success != true) || (resp.get_StatusCode() != 201)) {
        System.out.println("HTTP " + resp.get_StatusCode());
        if ((parsed == true) && (reply.HasMember("code") == true)) {
            System.out.println("Twilio error " + reply.IntOf("code") + ": " + reply.stringOf("message"));
            }

        return;
        }

    if (parsed != true) {
        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"));
  }
}