How to Send SMS without a Phone Number using C# .NET and an Alphanumeric Sender ID

Niels Swimburger.NET 🍔 - Jun 27 '22 - - Dev Community

This blog post was written for Twilio and originally published at the Twilio blog.

You can quickly buy a phone number using Twilio's Super Network and then use that phone number to send and receive SMS. However, not all applications require two-way messaging. What's more, there are a lot of use cases where one-way messaging is a better fit, for example: notifications, alerts, and verifications. For these kinds of scenarios, you should consider using an Alphanumeric Sender ID.

In this tutorial, you will learn how to send SMS messages from an Alphanumeric Sender ID with C#, .NET, and Twilio Programmable SMS; but first…

What is an Alphanumeric Sender ID

An Alphanumeric Sender ID (or Alpha Sender) lets you send SMS messages from a sender ID that consists of a set of alphanumeric characters, instead of from a phone number.

The biggest benefit of using Alpha Sender is that you can use an ID that is more recognizable to your users than a phone number. The example below shows how an Alpha Sender (AUTHMSG) would look instead of the standard number.

Two SMS conversations side-by-side. The SMS on the left originate from a phone number and the conversation on the right originates from an Alphanumeric Sender ID "AUTHMSG"

There are a couple of things you should know about Alpha Sender:

  • The ID can contain up to 11 characters, composed of upper and lower case letters (A-Z), numbers (0-9), and spaces.
  • A single Alpha Sender can be used across many countries without needing a dedicated phone number for each — but not all countries support Alpha Sender, and some countries require pre-registration.
  • Recipients cannot respond to an SMS sent from an Alpha Sender, making it a one-way conversation from the Alpha Sender to the recipient.

For other benefits, limitations, and guidance, read the support article about Alphanumeric Sender ID for Twilio Programmable SMS

Prerequisites

You will need these things to follow along with this tutorial:

You can find the source code for this tutorial on GitHub. Use it if you run into any issues, or submit an issue if you run into problems.

Configure an Alphanumeric Sender ID in Twilio

The recommended way to send SMS from an Alpha Sender is to use a Messaging Service in Twilio. To create a Messaging Service, log into the Twilio Console and click Explore Products in the left hand side navigation menu. On the Explore Products page, find and click the Messaging product. Then, in the left hand side navigation menu, click on Services under the Messaging submenu. On the Messaging Services page, click Create Messaging Service which leads you to a multi-step wizard.

On Step 1 , enter a meaningful name into the Messaging Service friendly name , and click Create Messaging Service.

Step 1 "Create Messaging Service" of the Messaging Service Setup wizard. This step has a form with one required field: Messaging Service friendly name. There"s a "Create Messaging Service" button which takes you to the next step.

On Step 2 , select the " Alpha Sender" option for the Sender Type dropdown, and click Continue. Next, enter your desired ID into the Alpha Sender ID field. This will be the ID shown to the recipient instead of a phone number.

A form to add an Alphanumeric Sender ID to the senders of a messaging service. The form contains one required field, the Alpha Sender ID field.

Click Add Alpha Sender and then click " Step 3: Set up integration".

Step 3 lets you configure how to handle incoming messages. Alpha Senders can't receive messages so you can ignore this step and go to " Step 4: Add compliance info".

In Step 4 , follow the described steps to add compliance info, if required for your use-case.

Finally, click Complete Messaging Service Setup.

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.

There will be a cURL command that you can copy/paste to your terminal. If you use the test interface or the cURL command to send an SMS to your phone number, you should receive an SMS from your Alpha Sender ID. Notice that you can't reply.

Get Your Twilio Configuration

You'll need some configuration elements from Twilio to send text messages from C#. Back in the Twilio Console, click on the Properties link under your Messaging Service in the left navigation menu, then take note of your Messaging Service SID. You will need it later.

Then, click on your account link on the top left to navigate back to the Twilio Console start page, and take note of your Twilio Account SID and Auth Token located at the bottom left of the page.

Account Info box holding 3 read-only fields: Account SID field, Auth Token field, and Twilio phone number field.

You need to configure this Twilio configuration as environment variables because the project will need them. Open your preferred shell, and set these environment variables using the following commands.

If you're using Bash or a similar shell:

export ToPhoneNumber=[RECIPIENT_PHONE_NUMBER]
export TwilioAccountSid=[TWILIO_ACCOUNT_SID]
export TwilioAuthToken=[TWILIO_AUTH_TOKEN]
export TwilioMessagingServiceSid=[TWILIO_MESSAGING_SERVICE_SID]
Enter fullscreen mode Exit fullscreen mode

If you're using PowerShell:

$Env:ToPhoneNumber = "[RECIPIENT_PHONE_NUMBER]"
$Env:TwilioAccountSid = "[TWILIO_ACCOUNT_SID]"
$Env:TwilioAuthToken = "[TWILIO_AUTH_TOKEN]"
$Env:TwilioMessagingServiceSid = "[TWILIO_MESSAGING_SERVICE_SID]"
Enter fullscreen mode Exit fullscreen mode

If you're using CMD:

set "ToPhoneNumber=[RECIPIENT_PHONE_NUMBER]"
set "TwilioAccountSid=[TWILIO_ACCOUNT_SID]"
set "TwilioAuthToken=[TWILIO_AUTH_TOKEN]"
set "TwilioMessagingServiceSid=[TWILIO_MESSAGING_SERVICE_SID]"
Enter fullscreen mode Exit fullscreen mode

In each case:

Send SMS from Alphanumeric Sender ID with C# and .NET

With that configuration done, it's time to write some C# code! and create a new console project using the .NET CLI, by running the following command:

dotnet new console -o SendAlphaSms
Enter fullscreen mode Exit fullscreen mode

This will create a new console project in a new folder named SendAlphaSms. Navigate to the SendAlphaSms folder by running the following command:

cd SendAlphaSms
Enter fullscreen mode Exit fullscreen mode

The best way to start using Twilio in .NET is to add the Twilio SDK for C# and .NET. Add the Twilio NuGet package using the .NET CLI:

dotnet add package Twilio
Enter fullscreen mode Exit fullscreen mode

Open the project in your preferred .NET editor and open the Program.cs file. Then, replace the existing code with the following:

using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

var toPhoneNumber = Environment.GetEnvironmentVariable("ToPhoneNumber");
var twilioAccountSid = Environment.GetEnvironmentVariable("TwilioAccountSid");
var twilioAuthToken = Environment.GetEnvironmentVariable("TwilioAuthToken");
var twilioMessagingServiceSid = Environment.GetEnvironmentVariable("TwilioMessagingServiceSid");

// authenticate with Twilio
TwilioClient.Init(twilioAccountSid, twilioAuthToken);

// send SMS using a Messaging Service
MessageResource.Create(
        to: new PhoneNumber(toPhoneNumber),
        messagingServiceSid: twilioMessagingServiceSid,
        body: "Hello from your Alpha sender 👏"
);
Enter fullscreen mode Exit fullscreen mode

The code grabs the configuration from the environment variables, then authenticates with Twilio, and finally sends an SMS using your Messaging Service, which will use your Alpha Sender.

Try it out by running the application using the .NET CLI:

dotnet run
Enter fullscreen mode Exit fullscreen mode

If you entered your phone number, you will now receive a text message from your Alpha Sender! Great job 🎉

Next steps

Congratulations on learning how to send SMS from an Alpha Sender with .NET and C#! If you run into any problems, you can refer to the source code for this tutorial on GitHub, or you can submit an issue on the GitHub repository. In addition to the above sample code, the source code also contains a sample for sending SMS from an Alpha Sender without a Messaging Service.

We can't wait to see what you'll build with Twilio and Alphanumeric Sender IDs!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .