Sending SMS Messages in ASP.NET With C#



Last updated: January 28th, 2024

Recently I was given the task to send SMS messages to a cell phone when new orders come into one of my sites. The goal was that immediately after a new order arrived, a text message needed to be sent to a Google voice number with the order number and a link to the order details on the website. At first I wasn't sure exactly if this was going to be easy but it turned out to be something I had working in less than thirty minutes.

I found a company called Nexmo that allows a free sign up to send SMS messages, after I signed up I got a username and password to use. Simply using an HTTP GET request allows a text message to be sent with a JSON response.
This is the URL Format:

http://rest.nexmo.com/sms/json?username={0}&password={1}&from={2}&to={3}&text={4}

The username and password were given but the “from” parameter actually turned out to be a phone number that they refer to as “MSISDN”. To get that number I had to select my country, United States, and then select a number to send from. The “to” parameter is the phone number to send to in international format, meaning that to send to a US phone number required prefixing it with a “1”. Both the “from” and “to” are plain numbers with no spaces. The “text” parameter is the actual text message to be sent from the number in the “from” parameter.
From this I was able to modify the sample C# code they have to allow me to invoke the SMS method through a static method call:
To invoke the method I can simply use:

Operational.SMS.SMSSender.SendMessage("13051234567", "My text message");

This allows me to send an SMS message with overload options and get back the JSON as an object to use for other purposes if I want. I use a static class and read in app settings to store the username, password and from phone number (for perfomance and maintenance reasons). This is a really easy way to integrate SMS into my application. They give you a small amount of money to test with, the from number costs a small monthly rate (around $1/ month) and each message costs somewhere around 1 US cent. The costs vary by country but for my purpose of just sending to a single US number from a single US number, this works well.

Comments

No Comments

Post Comment

Prove you are human 6 + 13 =

Tagged: ASP.NET, C Sharp


Join my email list!



ryan
About Me

With 15 years in tech, I've excelled as a senior software engineer, specializing in ASP.NET, C#, SQL, Azure, and front-end technologies. I've led diverse projects across various sectors, from startups to global corporations, particularly during my decade in the San Francisco Bay Area.


Sign Up With SoftSys Hosting! (My host)