SMS API Integration Using C#

In this blog, we are going to discuss how to send SMS directly using a C# application.

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,
  1. 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
  1. public static string SendSMS(string MobileNumber, string Message)  
  2. {  
  3.   
  4.     //Your authentication key  
  5.     string authKey = "";  
  6.        
  7.     //Multiple mobiles numbers separated by comma  
  8.     string mobileNumber = MobileNumber;  
  9.     //Sender ID,While using route4 sender id should be 6 characters long.  
  10.     string senderId = "TESTIN";  
  11.     //Your message to send, Add URL encoding here.  
  12.        
  13.     string message = HttpUtility.UrlEncode(Message);  
  14.     string route = "4";  
  15.     //Prepare you post parameters  
  16.     StringBuilder sbPostData = new StringBuilder();  
  17.     sbPostData.AppendFormat("authkey={0}", authKey);  
  18.     sbPostData.AppendFormat("&mobiles={0}", mobileNumber);  
  19.     sbPostData.AppendFormat("&message={0}", message);  
  20.     sbPostData.AppendFormat("&sender={0}", senderId);  
  21.     sbPostData.AppendFormat("&route={0}", route);  
  22.   
  23.   
  24.     //Call Send SMS API  
  25.     string sendSMSUri = "https://control.msg91.com/api/sendhttp.php";  
  26.     //Create HTTPWebrequest  
  27.     HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(sendSMSUri);  
  28.     //Prepare and Add URL Encoded data  
  29.     UTF8Encoding encoding = new UTF8Encoding();  
  30.     byte[] data = encoding.GetBytes(sbPostData.ToString());  
  31.     //Specify post method  
  32.     httpWReq.Method = "POST";  
  33.     httpWReq.ContentType = "application/x-www-form-urlencoded";  
  34.     httpWReq.ContentLength = data.Length;  
  35.     using (Stream stream = httpWReq.GetRequestStream())  
  36.     {  
  37.         stream.Write(data, 0, data.Length);  
  38.     }  
  39.     //Get the response  
  40.     HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();  
  41.     StreamReader reader = new StreamReader(response.GetResponseStream());  
  42.     string responseString = reader.ReadToEnd();  
  43.   
  44.     //Close the response  
  45.     reader.Close();  
  46.   
  47.     response.Close();  
  48.     return responseString;  
  49.   

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.