How to send SMS Without a Phone Number using Alpha Sender and Java

March 15, 2022
Written by
Reviewed by
Diane Phan
Twilion

How to send SMS Without a Phone Number using Alpha Sender and Java

SMS is useful for two-way messaging – you can chat with your friends and engage with businesses. There are also a lot of use cases where one-way messages are handy, for example: notifications, alerts, verifications. For this kind of message you should consider using an Alphanumeric Sender ID.

Some things to know about using an Alpha Sender:

  • A message identifies the sender by name, without the recipient needing to have a phone number in their address book already.
  • A single Alpha Sender can be used across many countries without needing a dedicated phone number for each.
  • It is not possible to reply to a message from an Alpha Sender. Pro: you don't need to handle incoming messages, Con: not suitable for use cases that need conversations.

An incoming message from an Alpha Sender called "MatthewTwlo" will look like this:

Screenshot of a messaging app showing a message from an Alpha Sender called "MatthewTwlo".

It isn't possible to send an SMS to an Alpha Sender, so my messaging app shows this message with no option to reply.

What you'll need to send Alpha Sender SMS

How to set up an Alpha Sender

The recommended way to configure sending from an Alpha Sender is to set up a Messaging Service. It is possible to send Alpha Sender messages without a Messaging Service but we won't cover that here. Setting up a Messaging Service only takes a minute and will be more flexible in the future. Create the Messaging Service from the Twilio console menus:

Messaging > Services > "Create Messaging Service"

Create and configure a Messaging Service

Step 1

Give your Messaging Service a name:

Screenshot: Messaging Service Setup stage 1.

Step 2

Add a sender to your new Messaging Service, selecting "Alpha Sender" as the Sender Type:

Screenshot of adding an Alpha Sender to a Messaging Service

On this dialog are links to Alphanumeric Sender ID documentation and details of international support for Alpha Senders.

Next set the actual Sender ID. This will appear in recipients’ messaging apps, as seen in the example in the beginning.

Screenshot: "Add Senders" adding an Alpha Sender called "MatthewTwlo"

You are limited to 11 alphanumeric characters for the Sender ID.

Click on “Setup Integration”

Step 3

Set up integration: This relates to how incoming messages are handled by the Messaging Service, but Alpha Senders can't receive messages so you can click through to step 4 straight away.

Step 4

If you are sending Application-to-Person SMS in the USA you will be subject to A2P10DLC rules. I won't cover that here, but if it applies to you then follow this link to find out more and register. Click on the Complete Messaging Service Setup button.

Finish the setup

Click Complete Messaging Service Setup to finish setting up your Messaging Service and Alpha Sender.

You will be prompted to test from the console. (You can also get to this by navigating the console menus: Messaging > Try it out > Send an SMS) You may see a warning message that no phone numbers are configured for this messaging service which you can safely ignore - after all, when using an Alpha Sender, not having a phone number is expected.

There will be a curl command that you can Copy/Paste to your terminal. Put your Auth Token in. You'll receive an SMS to your phone from your Alpha Sender ID.  Notice that you can't reply.

Send SMS from Alpha Senders in Java Code

Now you have set up everything you need in your Twilio account, it's time to write some code. If you want to skip ahead, the full project is available on GitHub.

Create a new Maven project

Create a new Maven project in your IDE. One quick way to do this is to use your IDE's New Project wizard. Once you have a Maven project set up, you're ready to start coding.

Use a recent version of Java

By default Maven may assume you are using an old version of Java. Update the project to the latest version with the following snippet of XML in pom.xml if it's not there already:

    <properties>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
    </properties>

The Twilio helper library requires at least Java 8, and 11 is the most widely used at the moment.

Add the Twilio Helper Library to your project

Add a dependency on the Twilio helper library by adding the following to the <dependencies> section of your pom.xml. Create the top-level <dependencies> section if it's not there with the following lines:

        <dependency>
            <groupId>com.twilio.sdk</groupId>
            <artifactId>twilio</artifactId>
            <version>8.27.0</version>
        </dependency>

We always recommend using the latest version of the helper libraries. At the time of writing this is 8.27.0. Newer versions are released frequently and you can always check MvnRepository for updates.

These changes should leave your pom.xml looking like this.

Write a little Java

Create a new class called AlphaSender in src/main/java. Paste the following content:

import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;

public class AlphaSender {

    public static void main(String[] args) {

        Twilio.init(
            System.getenv("TWILIO_ACCOUNT_SID"),
            System.getenv("TWILIO_AUTH_TOKEN"));

        Message.creator(
            new PhoneNumber(System.getenv("DESTINATION_NUMBER")),
            System.getenv("MSG_SVC_SID"),
            "Hello from your Alpha sender 😊")
            .create();
    }
}

[this code on GitHub]

This code relies on 4 environment variables:

  • TWILIO_ACCOUNT_SID Find this on your Twilio Console.
  • TWILIO_AUTH_TOKEN Find this on your Twilio Console.
  • DESTINATION_NUMBER The phone number you want to send to. Use E.164 format.
  • MSG_SVC_SID The ID of your Messaging Service. You will find this on the Twilio console page where you created the Messaging Service above.

Using environment variables avoids the need to hard-code sensitive information which is especially important if you are adding this code to any source control repo. There are IDE plugins to make this manageable- I use Env File for IntelliJ IDEA.

Once you have saved the code and set up the environment variables, you can run this class directly from your IDE. You may see warnings about unconfigured logging libraries, which you can ignore for this example (in a larger app you should configure logging properly). The phone you put as DESTINATION_NUMBER will buzz. Congratulations, you've used an Alpha Sender to send SMS!

Wrap up

If you made it this far, congratulations! You've sent an SMS from an Alpha Sender - but what's next is up to you. I'd love to hear how you're using Twilio and Java, get in touch with me to talk about what you're building or ask me anything.