Introduction
In this blog, we are going to discuss how to send SMS using third party APIs.
Step 1
First we need to create an account. Please click on the below link and complete your registration process.
Step 2
After login, we need to create API key. Please check the below figure.
Step 3
Let's discuss about API.
We need to consume the below API with proper query parameters.
The API URL is,
- https://api.msg91.com/api/sendhttp.php?mobiles=Mobile no.&authkey=$authentication_key&route=4&sender=TESTIN&message=Hello! This is a test message&country=91
Query parameters are:
- mobiles - In this parameter, we need to pass mobile number.
- authkey - In this parameter, we need to pass the API key.
- route - This parameter differentiates the SMS type such as transactional or promotional
- sender - Receiver will see this as sender id.
- message - In this parameter, we need to pass the actual message.
By using the below code we can send SMS to the respective mobile number.
Code Snippet
- public static string SendSMS(string MobileNumber, string Message)
- {
- //Your authentication key
- string authKey = "";
- //Multiple mobiles numbers separated by comma
- string mobileNumber = MobileNumber;
- //Sender ID,While using route4 sender id should be 6 characters long.
- string senderId = "TESTIN";
- //Your message to send, Add URL encoding here.
- string message = HttpUtility.UrlEncode(Message);
- string route = "4";
- //Prepare you post parameters
- StringBuilder sbPostData = new StringBuilder();
- sbPostData.AppendFormat("authkey={0}", authKey);
- sbPostData.AppendFormat("&mobiles={0}", mobileNumber);
- sbPostData.AppendFormat("&message={0}", message);
- sbPostData.AppendFormat("&sender={0}", senderId);
- sbPostData.AppendFormat("&route={0}", route);
- //Call Send SMS API
- string sendSMSUri = "https://control.msg91.com/api/sendhttp.php";
- //Create HTTPWebrequest
- HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(sendSMSUri);
- //Prepare and Add URL Encoded data
- UTF8Encoding encoding = new UTF8Encoding();
- byte[] data = encoding.GetBytes(sbPostData.ToString());
- //Specify post method
- httpWReq.Method = "POST";
- httpWReq.ContentType = "application/x-www-form-urlencoded";
- httpWReq.ContentLength = data.Length;
- using (Stream stream = httpWReq.GetRequestStream())
- {
- stream.Write(data, 0, data.Length);
- }
- //Get the response
- HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
- StreamReader reader = new StreamReader(response.GetResponseStream());
- string responseString = reader.ReadToEnd();
- //Close the response
- reader.Close();
- response.Close();
- return responseString;
- }
Summary
In this blog, we learned how to send SMS directly using a C# application by integrating MSG91 SMS API.
I hope you found it useful. Eat-> Code->Sleep->Repeat.

yuvaraj kPosted Oct 12, 2022, 5:02 AM
How to send regional languge wise SMS
Jaimin ShethiyaPosted Oct 22, 2019, 5:09 AM
Nice one :)
SoNuPosted Oct 14, 2019, 3:40 AM
Informative , thanks :)
Pankajkumar PatelPosted Oct 12, 2019, 1:59 AM
Nice article
Rajanikant HawaldarPosted Oct 11, 2019, 5:18 AM
Informative blog Madan