SMS - Tutorial: Send an SMS in C# using the Telesign SDK

This tutorial teaches you how to use the Telesign C# SDK to send an SMS. The complete sample code for this tutorial is available on GitHub here.

Before you begin

Make sure you have the following before you start:

  • Authentication credentials: Your Customer ID and API Key. If you need help finding these items, go to the support article How do I find my Customer ID and API Key.
  • Testing device: A mobile phone on which you can receive SMS.

📘

NOTE:

This tutorial uses the following:

  • MacOS
  • .NET Framework v6
  • dotnet CLI v6.0.400

Please modify accordingly if your developer environment differs from these details.

Set up your project

  1. Follow the Telesign Self-service C# SDK install instructions on GitHub here, incorporating the following details:
  • Use sms as the project name.
  • Use SendSMS as the solution name.

You should end up in the top-level directory (sms) for your project in the Terminal.

📘

NOTE:

Even if you have installed the C# SDK on your system before, you still need to add the package to this new C# solution.

  1. Enter the top-level directory for the new solution you just created.
cd SendSMS
  1. Open the file Program.cs in your editor.
open Program.cs

Create code to send an SMS

  1. Import the required namespaces and classes.
using System;
using Telesign;
  1. Create the basic structure of the script, including the Main function. In the next step, you will begin adding statements to the Main function.
namespace SendSMS
{
    class SendSMS
    {
        static void Main(string[] args)
        {

        }
    }
}
  1. Define variables in the Main function to store your authentication credentials. For testing purposes, you can just overwrite the default values below rather than use environment variables.
string customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
string apiKey = "ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
  1. Define a variable to hold the recipient's phone number. For this tutorial, hardcode your testing device's phone number.
string phoneNumber = "1234567890";
  1. (Optional) Pull authentication credentials from environment variables. Although you might not choose to do so for this tutorial, in your production integration it's a good practice to reference your authentication credentials using environment variables.
if (System.Environment.GetEnvironmentVariable("CUSTOMER_ID") != null) {
    customerId = System.Environment.GetEnvironmentVariable("CUSTOMER_ID");
}

if (System.Environment.GetEnvironmentVariable("API_KEY") != null) {
    apiKey = System.Environment.GetEnvironmentVariable("API_KEY");
}

if (System.Environment.GetEnvironmentVariable("PHONE_NUMBER") != null) {
    phoneNumber = System.Environment.GetEnvironmentVariable("PHONE_NUMBER");
}
  1. Define variables for the message text and the Telesign abbreviation describing the message type. For purposes of this tutorial, use the "ARN" abbreviation (alerts, reminders, and notifications).
string message = "Hello world!";
string messageType = "ARN";
  1. Create a try-catch structure.
try
{

}
catch (Exception e)
{
    
}

  1. In the try block, add code to instantiate a Telesign messaging client object with your authentication credentials.
try
{
    MessagingClient messagingClient = new MessagingClient(customerId, apiKey);
}
  1. Next in the try block, add code to make the request and capture the response.
RestClient.TelesignResponse telesignResponse = messagingClient.Message(phoneNumber, message, messageType);
  1. In the catch block, add code to print any error that might occur.
Console.WriteLine(e);
  1. After the try-catch structure, add code to allow you to exit the program after it finishes attempting to send the SMS.
Console.WriteLine("Press any key to quit.");
Console.ReadKey();

return;

Sample code

The complete sample code for this tutorial can be found on GitHub.