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
-
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.
- Use
-
Enter the top-level directory for the new solution you just created.
cd SendSMS
-
Open the file "Program.cs" in your editor.
open Program.cs
Create code to send an SMS
-
Import the required namespaces and classes.
using System; using Telesign;
-
Create the basic structure of the script, including the
Main
function. In the next step, you will begin adding statements to theMain
function.namespace SendSMS { class SendSMS { static void Main(string[] args) { } } }
-
Define variables in the
Main
function to store your authentication credentials. For testing purposes, you can just overwrite the default values below or use environment variables (see step 5).string customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890"; string apiKey = "ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
-
Define a variable to hold the recipient's phone number. For this tutorial, hardcode your testing device's phone number.
string phoneNumber = "11234567890";
-
(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"); }
-
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 = "Your package has shipped! Follow your delivery at https://vero-finto.com/orders/3456"; string messageType = "ARN";
-
Create a try-catch structure.
try { } catch (Exception e) { }
-
In the
try
block, add code to instantiate a Telesign messaging client object with your authentication credentials.try { MessagingClient messagingClient = new MessagingClient(customerId, apiKey); }
-
Next in the
try
block, add code to make the request and capture the response.RestClient.TelesignResponse telesignResponse = messagingClient.Message(phoneNumber, message, messageType);
-
Next in the
try
block, add code to print the response for debugging purposes. In your production code you would likely remove this.Console.WriteLine("\nResponse HTTP status:\n" + telesignResponse.StatusCode); Console.WriteLine("\nResponse body:\n" + telesignResponse.Body);
-
In the
catch
block, add code to print any error that might occur.Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("\nAn exception occured.\nERROR: " + e.Message + "\n"); Console.ResetColor();
-
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.
Updated 12 months ago