Send an MMS

In this code snippet you will see how to send an MMS using the Messages API.

IMPORTANT: Only US short codes, 10DLC numbers and SMS Enabled Toll Free Numbers are currently supported for sending MMS. For US short codes, MMS messages can be sent to AT&T, T-Mobile (previously Sprint), and Verizon networks in the US. Find out more about setting up 10DLC numbers (note: this page references the SMS API, but the contents of the 10 DLC guidelines section also apply to the Messages API).

Message throughput, deliverability, and SMS message volumes may vary depending on the type of number used. For more information on this, and on MMS in general, see the Vonage MMS overview page, the Vonage 10DLC overview page, and the Vonage Phone Numbers overview page.

Example

Find the description for all variables used in each code snippet below:

Key Description
VONAGE_APPLICATION_ID The Vonage Application ID.
VONAGE_APPLICATION_PRIVATE_KEY_PATH Private key path.
VONAGE_PRIVATE_KEY_PATH Private key path.
FROM_NUMBER The phone number you are sending the MMS from. (US Short Code, 10DLC number, or SMS Enabled Toll Free Number)
VONAGE_NUMBER Refer to FROM_NUMBER above
VONAGE_FROM_NUMBER Refer to FROM_NUMBER above
TO_NUMBER The number you are sending the to in E.164 format. For example 447700900000.
IMAGE_URL The URL of the media you want to send. Accepted file formats are .jpg, .jpeg, .png, and .gif.

NOTE: Don't use a leading + or 00 when entering a phone number, start with the country code, for example 14155550105.

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

Write the code

Add the following to send-mms.sh:

curl -X POST https://api.nexmo.com/v1/messages \
  -H 'Authorization: Bearer '$JWT\
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -d $'{
            "message_type": "image",
            "image": {
                "url": "https://example.com/image.jpg"
            },
            "to": "$TO_NUMBER",
            "from": "$FROM_NUMBER",
            "channel": "mms"
}'

View full source

Run your code

Save this file to your machine and run it:

bash send-mms.sh

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

npm install @vonage/server-sdk

Create a file named send-mms.js and add the following code:

const Vonage = require('@vonage/server-sdk')
const MMSImage = require('@vonage/server-sdk/lib/Messages/MMSImage')

const vonage = new Vonage({
  apiKey: VONAGE_API_KEY,
  apiSecret: VONAGE_API_SECRET,
  applicationId: VONAGE_APPLICATION_ID,
  privateKey: VONAGE_APPLICATION_PRIVATE_KEY_PATH
})

View full source

Write the code

Add the following to send-mms.js:

vonage.messages.send(
  new MMSImage({ "url": IMAGE_URL }, TO_NUMBER, FROM_NUMBER),
  (err, data) => {
    if (err) {
      console.error(err);
    } else {
      console.log(data.message_uuid);
    }
  }
);

View full source

Run your code

Save this file to your machine and run it:

node send-mms.js

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

Add the following to `build.gradle`:

compile 'com.vonage:client:6.2.0'

Create a class named SendMmsImage and add the following code to the main method:

VonageClient client = VonageClient.builder()
        .applicationId(VONAGE_APPLICATION_ID)
        .privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
        .build();

MessagesClient messagesClient = client.getMessagesClient();

View full source

Write the code

Add the following to the main method of the SendMmsImage class:

var message = MmsImageRequest.builder()
        .from(FROM_NUMBER).to(TO_NUMBER)
        .url("https://file-examples.com/wp-content/uploads/2017/10/file_example_GIF_500kB.gif")
        .caption("Accompanying message (optional)")
        .build();

try {
    MessageResponse response = messagesClient.sendMessage(message);
    System.out.println("Message sent successfully. ID: "+response.getMessageUuid());
}
catch (MessageResponseException mrx) {
    switch (mrx.getStatusCode()) {
        default: throw mrx;
        case 401: // Bad credentials
            throw new IllegalStateException(mrx.getTitle(), mrx);
        case 422: // Invalid
            throw new IllegalStateException(mrx.getDetail(), mrx);
        case 402: // Low balance
            client.getAccountClient().topUp("transactionID");
            break;
        case 429: // Rate limit
            Thread.sleep(12_000);
            break;
    }
}

View full source

Run your code

We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:

  apply plugin: 'application'
  mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Run the following gradle command to execute your application, replacing com.vonage.quickstart.messages.mms with the package containing SendMmsImage:

gradle run -Pmain=com.vonage.quickstart.messages.mms.SendMmsImage

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

Install-Package Vonage

Write the code

Add the following to SendMmsImage.cs:

var credentials = Credentials.FromApiKeyAndSecret(
    apiKey,
    apiSecret
);

var vonageClient = new VonageClient(credentials);

var request = new Vonage.Messages.Mms.MmsImageRequest
{
    To = to,
    From = brandName,
    Image = new Attachment
    {
        Url = "https://example.com/image.jpg"
    }
};

var response = await vonageClient.MessagesClient.SendAsync(request);

View full source

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

pip install vonage

Write the code

Add the following to send_image.py:

client = vonage.Client(
    application_id=VONAGE_APPLICATION_ID,
    private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
)

client.messages.send_message(
    {
        "channel": "mms",
        "message_type": "image",
        "to": TO_NUMBER,
        "from": FROM_NUMBER,
        "image": {
            "url": "https://www.example.com/image.jpg",
            "caption": "Test image sent via MMS with Vonage's Messages API",
        },
    }
)

View full source

Run your code

Save this file to your machine and run it:

python3 send_image.py

Prerequisites

If you do not have an application you can create one. Make sure you also configure your webhooks.

gem install vonage

Write the code

Add the following to send-mms.rb:

client = Vonage::Client.new(
  application_id: VONAGE_APPLICATION_ID,
  private_key: File.read(VONAGE_APPLICATION_PRIVATE_KEY_PATH)
)

message = Vonage::Messaging::Message.mms(
  type: 'image',
  message: {
    url: "https://example.com/image.jpg",
    caption: "A MMS image message, with caption, sent using the Vonage Messages API"
  }
)

client.messaging.send(
  from: VONAGE_FROM_NUMBER,
  to: TO_NUMBER,
  **message
)

View full source

Run your code

Save this file to your machine and run it:

ruby send-mms.rb

Try it out

When you run the code an MMS message is sent to the destination number.