Send a Text Message using .NET Core 1.0 in 15 seconds

Mr. Hanselman said .Net 5 is dead. ASP.NET 5 is now ASP.NET Core 1.0 and Entity Framework 7 is now Entity Framework Core 1.0. With the whole new core concept Microsoft is doing a good job keeping things interesting with new and old .Net followers. With Visual studio code you can now run C-sharp and vb.net both on Mac it seems.

So let’s get adventurous here and learn something new. Let’s say you are build the next Uber and want to notify your customers that Your uber is arriving soon via a text message. How would you do that.

Well we’ll leave the logic of calculating the distance between the passenger and the driver up to you, what we are focussing on here is how you’d send a text message programmatically when your logic needs to trigger one.

PureText offers a Texting/SMS gateway to which you can make an HTTP GET/POST request with details such as recipient number and body 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

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

  1. An API token key  (free 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 from the ones in your dashboard

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", "+14703497270" },
                            //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
    }
}