Sending outbound SMS messages with MessageBird

⏱ 5 min build time

In this MessageBird Developer Tutorial you’ll learn how to send your first SMS using the MessageBird SMS Messaging API.

Before we get started, have you set up your Java development environment and project directory with the MessageBird SDK?

  • No! Make sure you to set this up before continuing; you can read this MessageBird Developer Tutorial to learn how to do so.
  • Yes! Great! Now you can make your first API request and send an SMS message with MessageBird using Java.

Getting started

First, let's create a new file in the directory of your src/main/java folder and call it SMSQuickstart.java.

In this file, let's include the MessageBird SDK using import. The SDK expects a single argument, your API key. Next, to start testing the SDK, you can simply replace the string YOUR-API-KEY in the following Java code and hardcode your API key.

Pro-tip: For production applications, we recommended storing the key in a configuration file or environment variable instead, and passing this variable with the key to the client. You'll see this in practice later in our MessageBird Developer Tutorials for building common use cases.

// Create a MessageBirdService
final MessageBirdService messageBirdService = new MessageBirdServiceImpl("YOUR-API-KEY");
// Add the service to the client
final MessageBirdClient messageBirdClient = new MessageBirdClient(messageBirdService);

Now, to send a message with the SDK, we call message_create on the SDK object and pass a few required arguments:

// convert String number into acceptable format
BigInteger phoneNumber = new BigInteger("31970YYYYYYY");
final List<BigInteger> phones = new ArrayList<BigInteger>();
phones.add(phoneNumber);
final MessageResponse response = messageBirdClient.sendMessage("31970XXXXXXX", "Hello World, I am a text message and I was hatched by Java code!", phones);

But wait, what do these attributes mean? 🤔

  • The originator is the sender for the message; it can be a telephone number including country code or an alphanumeric string (with a maximum length of 11 characters). You can use the number you bought as part of our Getting Started tutorialas originator. Keep in mind that alphanumeric senders are not supported in every country including the United States, so it’s important to check the country restrictions. If you can't use alphanumeric IDs, use a real phone number instead. You can check our originator article in Help Center to learn more about this topic.

  • The recipients are the phone numbers that will receive the message. All numbers should be in the international format with country code. You must specify this attribute as an array even if you have just a single recipient. You can send a message to up to 50 numbers at a time. For testing, add your own mobile telephone number here so you can receive the test message on your phone.

  • The body is the content of the message. Keep in mind that SMS messages are limited to 160 characters, and if you enter a longer value for the body it will be split into multiple parts which are each sent and billed separately.

Make sure to replace the values in the sample code with adequate data for testing. There are additional optional attributes as well; you can find them in our SMS Messaging API documentation.

If this call fails, the MessageBird client throws an UnauthorizedException or GeneralException error. We can catch this to provide more error information to a user if need be:

try {
final MessageResponse response = messageBirdClient.sendMessage("31970XXXXXXX", "Hello World, I am a text message and I was hatched by Java code!", phones);
} catch (UnauthorizedException | GeneralException ex) {
System.out.println(ex.toString())
}

Great! Your first Java app is ready. Let's go ahead and save it.

In production applications, you should not expose the raw values for error and provide user-friendly error handling instead, but the current implementation is great for testing.

Enough said, let's try running it from your IDE.

If everything works fine, you should see the API response as output from the script. If you used a live API key and verified your number or added test credits to your account the message will arrive on your phone in seconds.

Nice work! 🎉

You've just sent your first SMS message with MessageBird’s SMS Messaging API using Java!

Start building!

Head over to the next MessageBird Developer Tutorial and learn how to set up inbound messaging functionality.

Want to start building your solution but not quite sure how to get started? Feel free to let us know at support@messagebird.com; we'd love to help!

Questions?

We’re always happy to help with code or other doubts you might have! Check out our Quickstarts, API Reference, Tutorials, SDKs, or contact our Support team.

Cookie Settings