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.
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)
- {
-
-
- string authKey = "";
-
-
- string mobileNumber = MobileNumber;
-
- string senderId = "TESTIN";
-
-
- string message = HttpUtility.UrlEncode(Message);
- string route = "4";
-
- 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);
-
-
-
- string sendSMSUri = "https://control.msg91.com/api/sendhttp.php";
-
- HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(sendSMSUri);
-
- UTF8Encoding encoding = new UTF8Encoding();
- byte[] data = encoding.GetBytes(sbPostData.ToString());
-
- 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);
- }
-
- HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
- StreamReader reader = new StreamReader(response.GetResponseStream());
- string responseString = reader.ReadToEnd();
-
-
- 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.