javatwiliomms

How to send MMS message to phone number using Java and Twilio?


I wanted to send MMS message (image) to phone number using Java and Twilio.

How Can I send MMS message (Images) to phone number? Is it possible? Please let me know if you know better solution or better service 🙏🏻 Thank u

I created an account in Twilio and got SID, AUTH_TOKEN, and new phone number. Add this dependency below to my project. implementation 'com.twilio.sdk:twilio:9.2.5' and sent simple message to phone number at first using this code below

    public static void main(String[] args) {
        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

        Message message = Message.creator(
                        new PhoneNumber("+996*********"),
                        new PhoneNumber("+16*********"),
                        "Ahoy world! I love writing code in Java.")
                .create();

        System.out.println(message.getSid());
    }

it works perfectly

After I tried to send MMS message. I asked from ChatGPT and it gave me this code below

public static void main(String[] args) throws URISyntaxException {
        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

        Message message = Message.creator(
                        new PhoneNumber("+996552157541"),
                        new PhoneNumber("+16207054562"),
                        "Ahoy world! I love writing code in Java.")
                .setMediaUrl(
                        List.of(new URI("https://i.pinimg.com/474x/f5/36/01/f53601133f236d1cb167ac19f05a3d60.jpg"))
                )
                .create();

        System.out.println(message.getSid());
    }

it also works but not as I expected it sends image link instead of image


Solution

  • The way MMS is sent/received depends on the country you are sending MMS to.

    Based on the country code +996, it seems that you're sending an MMS to Kyrgyzstan. For that Kyrgyzstan, MMS are converted to SMS with embedded URL, as documented on the Kyrgyzstan SMS guidelines.