Jan 23rd, 2020

How to send a text using c-sharp

To send a text programmatically your code needs to make an XHR request to an SMS gateway that will read the text details from your request and then initiate a text to the recipient's cell number.

PureText offers a Texting/SMS gateway to which you can make an HTTP GET/POST request with details such as recipient number and content of the text and PureText will deliver the text to the recipient for you.

In order to identify that this request belongs to you, you'll need to include your API token in the HTTP GET/POST request.

Register for a free API token here

A thing to note is that you cannot just use any phone number as the 'From' number. The number must be registered through the same organization who's SMS gateway you choose to use.

To send a text message using C#.net or VB.NET you don't need a dedicated short code. 

Dedicated shortcodes are very expensive and take 2-3 months to get approved, instead, you can now use local 10 digit VOIP numbers, also referred to as "long codes" to send a text message programmatically from C#.Net or VB.Net.

So let's get started, to do a quick and dirty POC you'll need:

  1. An API token ( here)
  2. A 'From' number through your SMS gateway (Get a free one here)

Copy Paste the following code in your IDE and replace the API Token and From Number values with the ones you were provided in the dashboard

using System;
using System.Collections.Generic;
using System.Net.Http;

public class Program
{
    public static void Main()
    {
        String URI = "https://api.puretext.us/service/sms/send";
        String resultContent;

        using (var client = new HttpClient())
        {
            var values = new Dictionary < string, string > 
                        {
                            // To Number is the number you will be sending the text to
                            { "toNumber", "+X-XXX-XXX-XXXX" },
                            // From number is the number you will buy from your admin dashboard
                            { "fromNumber", "+X-XXX-XXX-XXXX" },
                            //Sign up for an account to get an API Token
                            { "apiToken", "XXXXX" },
                            // Text Content
                            { "smsBody", "SENDING TEXT USING .NET" }

                         };

            var content = new FormUrlEncodedContent(values);
            var result = client.PostAsync(URI, content).Result;
            resultContent = result.Content.ReadAsStringAsync().Result;
        }

        Console.Write(resultContent);
        Console.Read(); //to keep console window open if trying in visual studio
    }
}

Was the article useful?

Your email address will not be published. Required fields are marked *