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

Check message delivery status with Twilio

See more Twilio Examples

Fetch an outbound SMS or MMS using the Message SID returned by a live send. A queued message is waiting to be sent. A sent message has been accepted by the upstream carrier. Delivered means Twilio received delivery confirmation. Some routes provide no final delivery receipt.

Read the current status once per lookup. For failed or undelivered messages, display the available delivery error code and text for troubleshooting. A missing message or an HTTP authentication error is a separate result from a delivery failure.

Twilio documentation

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
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.
        // Load an API key for the Twilio account from private application configuration.
        Http http = new Http();
        http.setRequireSslCertVerify(true);
        http.setBasicAuth(true);
        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("YOUR_ACCOUNT_SID");
        url.append("/Messages");
        url.append("/");
        url.append("YOUR_MESSAGE_SID");
        url.append(".json");
        HttpResponse resp = new HttpResponse();
        JsonObject reply = new JsonObject();

        try {
            http.httpNoBody("GET", url.getAsString(), resp);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (resp.getStatusCode() == 0) {
            System.out.println("No HTTP response from Twilio.");
            return;
        }

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

            return;
        }

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

        if (reply.jsonTypeOf("status") != 1) {
            System.out.println("Message status missing from response.");
            return;
        }

        System.out.println("Delivery status: " + reply.stringOf("status"));
        if ((reply.stringOfEquals("status", "failed", true)) || (reply.stringOfEquals("status", "undelivered", true))) {
            if (reply.jsonTypeOf("error_code") == 2) {
                System.out.println("Delivery error code: " + reply.intOf("error_code"));
            }

            if (reply.jsonTypeOf("error_message") == 1) {
                System.out.println(reply.stringOf("error_message"));
            }
        }
    }
}