How to create our own SMS gateway using ASP.net C#
Rate this article
5.00 out of 5
Introduction

This project was done for the completion of my  UG Degree in Computer Science and past two years it was in the industry for commercial use. And  i like to remember Mr Shefeek MJ in this occasion for his innovative support for developing SMS gateway.[ Now he is working for wipro technologies as a project engineer] .

SMS Gateway


SMS gateway is an ex-changer of data from two different network protocols.it  is a telecommunications network facility for sending or receiving Short Message Service (SMS) transmissions to or from a telecommunications network that supports SMS. Most messages are eventually routed into the mobile phone networks. Many SMS gateways support media conversion from email and other formats.

Description

To send SMS from your pc you should need the following components .

  • A PC with USB or Parallel ports enabled.
  • A GSM Modem [ A GSM Mobile with Data cable or Bluetooth connected ] support AT Commands
  • ASP.net [.net frame 3.5 installed ]
What we are doing is ,
  • converting our text to Wireless format ie first convert the data into pdu[ Packet data unit ---> it must have a format
  • Message center number in the format --> Octet
  • Message in the format - HEX  and we have to encode data .
[For PDU Format specifications see my website http://www.memcpy.in/2012/12/sms-and-pdu-format.html].

 //The Function of this class is to generate PDU message from the available data
    //Input to this class = Message Centre NUmber,Receiver Number,Message in text format
    //Output  = SMS in pdu format
    //Functions in this class //1.SendMessage(string receiver, string msg)  ---->Entry point to this class
  1.  //2 .GeneratePdu(string receiver, string msg) ----> Generates SMS in PDU format using the other                    functions
  2.  //3.MessageToHex(string message)------>Function to convert message to Hexadecimal 8-bit octets
  3.  //4.getOctet(string receiver)-------->Function to Encrypt Receiver and Message Centre Number(SemiOctet)
  4. //5.SendToSMSEngine(string pdu,int length)----->Exit point from this class.It passes the final SMS in the                     PDU format to the SMS Engine
C# Source Code

using System;
using System.Collections.Generic;
using System.Text;

namespace SMS // Name space is for avoid code collision
{
   class SendSMS
    {
        SMSCOMMS SMSEngine;
        //Entry point to this class
        public void SendMessage(string receiver, string msg)
        {
            int length = msg.Length;
            try
            {
                string pdu;
                pdu = GeneratePdu(receiver, msg); // Generating PDU see next post for know pdu format
                //obj.receiveSMS(pdu);
                SendToSMSEngine(pdu,length);
            }
            catch (Exception ex)
            {
            }
        }

        //The function passes the final message in the pdu format to the SMS Engine
        public void SendToSMSEngine(string pdu,int length)
        {
            string x = "COM17";
            SMSEngine = new SMSCOMMS(ref x);
            SMSEngine.Open();
            SMSEngine.SendPDU(pdu,length);
            SMSEngine.Close();
        }

      //Function to generate PDU
        public string GeneratePdu(string receiver, string msg)
        {
            string pdu=string.Empty;
          
            try
            {
                string meta = "0791";
                string messagecentre = getOctet("919605100193"); // Dummy number[ My old num]
                string sendernum = getOctet(receiver);
                string hexMessage = MessageToHex(msg);
                hexMessage = hexMessage.Trim();
                string hexLength = (msg.Length).ToString("X");
                if (hexLength.Length == 1)
                {

                    hexLength = "0" + hexLength + "";
                }
                pdu = "0791" + messagecentre + "01000C91" + sendernum + "0000" + hexLength + hexMessage + "";
                pdu = pdu.Trim();
              
            }
            catch (Exception ex)
            {
            }
            return pdu;   
        }

        //Function to convert message to Hexadecimal 8-bit octets
        public string MessageToHex(string message)
        {
            string hexString=string.Empty;
            try
            {
                string msg = message;
                int[] a = new int[msg.Length * 8];
                string[] splitstring = new string[msg.Length];
                string[] hexValue = new string[1000];
                int pos = 0;


                //Split the Message
                for (int i = 0; i < msg.Length; i++)
                {
                    splitstring.SetValue(msg.Substring(i, 1), i);
                }

                //Convert to Binary
                string[] bin = new string[splitstring.Length];
                for (int i = 0; i < splitstring.Length; i++)
                {
                    a[i] = Convert.ToInt32(Convert.ToChar(splitstring[i].ToString()));
                    bin[i] = Convert.ToString(a[i], 2);
                    if (bin[i].Length == 6)
                    {
                        bin[i] = "0" + bin[i].ToString() + "";
                    }
                }
             
                //Convert to octect
                string[] oct = new string[100];
                int count = 1;
                for (int i = 0; i < bin.Length - 1; i++)
                {
                    string[] sub = new string[bin[i].Length];
                    //Take i+1 th binary value
                    for (int j = 0; j < bin[0].Length; j++)
                    {
                        sub.SetValue(bin[i + 1].Substring(j, 1), j);
                    }
                    string concat = string.Empty;

                    //Make the string to concatanate
                    for (int k = sub.Length - count; k < sub.Length; k++)
                    {
                        concat = "" + concat + sub[k].ToString() + "";
                    }
                    string octal = string.Empty;
                    octal = "" + octal + concat + "";
                    //Concatanation
                    for (int l = 0; l < bin[i].Length - (count - 1); l++)
                    {
                        sub.SetValue(bin[i].Substring(l, 1), l);
                        octal = "" + octal + sub[l].ToString() + "";
                    }
                    hexValue[pos] = "" + octal + "";

                    pos++;
                    count++;
                    if (count == 8)
                    {
                        count = 1;
                        i = i + 1;
                    }

                    //Last COlumn
                    octal = string.Empty;
                    if (i == bin.Length - 2)
                    {
                        for (int l = 0; l < bin[i].Length - (count - 1); l++)
                        {
                            sub.SetValue(bin[i + 1].Substring(l, 1), l);
                            octal = "" + octal + sub[l].ToString() + "";
                        }

                        hexValue[pos] = "" + octal + "";
                    }
                }

                //Convert to Hexadecimal
                for (int i = 0; i <= pos; i++)
                {
                    int decimalval = Convert.ToInt32(hexValue[i].ToString(), 2);
                    string h = decimalval.ToString("X");
                    if (h.Length == 1)
                    {
                        h = "0" + h + "";
                    }
                    hexString = " " + hexString + h + "";
                  
                }
            }
            catch (Exception ex)
            {
            }
            return hexString;
        }

        //Function to Encrypt Receiver and Message Centre Number(SemiOctet)
        public string getOctet(string receiver)
        {
            string[] receive = new string[receiver.Length + 1];
            string returnreceive = string.Empty;
            try
            {
              
                if (receiver.Length % 2 == 0)
                {
                    for (int i = 0; i < receiver.Length; i++)
                    {
                        receive.SetValue(receiver.Substring(i, 1), i);
                    }
                }
                else
                {
                    for (int i = 0; i < receiver.Length; i++)
                    {
                        receive.SetValue(receiver.Substring(i, 1), i);
                    }
                    receive[receive.Length - 1] = "F";
                }

                for (int i = 0; i < receive.Length; i++)
                {
                    string swap;
                    swap = receive[i].ToString();
                    receive[i] = receive[i + 1].ToString();
                    receive[i + 1] = swap;
                    i++;
                    if (i == receiver.Length - 1)
                    {
                        if (receiver.Length % 2 == 0)
                        {
                            i++;
                        }
                    }
                }
                for (int i = 0; i < receive.Length; i++)
                {
                    returnreceive = "" + returnreceive + receive[i].ToString() + "";

                    if (i == receiver.Length - 1)
                    {
                        if (receiver.Length % 2 == 0)
                        {
                            i++;
                        }
                    }

                }
            }
            catch (Exception ex)
            {

            }
            return returnreceive;
        }
    }
}

Conclusion
 You people can use this by satisfying all regulations and rules from TRAI [Telecom Regulatory Authority of India]. if you are using this in india

To work this code fine it needs another class called SMSCOMMS-  Try to develope this from your side . That class contains AT Commands and use of AT Commands . This is also required to send sms . 



About The Author
Johnson Augustine
I am Johnson Augustine [My friends calls me - J.A), Completed my Master of Computer Applications degree, and Currently working as a Software Engineer at KTS Infotech PVT LTD and Server Admin and security Engineer at Scibero Hosting and Cyber Solutions. I have expertise in  C/C++/Visual C++/G++/QT++,Com,Device Driver and embedded System Development. I have 4 Year Experience in ASP.net and Visual studio 2005,2008,2010 and MSSQL Server 2000,2005,2008 ,I have also deep knowledge in Oracle database,MySQL Database,C++ with SQLite, Android mobile development , PHP , Joomla,Word Press ,html,Javascript,Jquery,Ajax.CSS , Networking ,Cyber security, Ethical Hacking and familiar with most of the web hosting control panels like Plesk 9.5,Plesk 10.4.4,Plesk 11.0.9 . Cpanel/WHM,Hsphere,Dotnet panel , and have experience in Windows server 2003,2008,Cent Os , RHEL,Fedora etc .and also have some work experience in cloud environment. 

You can see my professional profile at http://linkedin.com/in/johnsontaugustine

Tektip profile [Popular Technical Discussion Website ] : http://www.tektips.in/AuthorDetails.aspx?AID=technogreenworld@gmail.com

See my personal technical discussion website http://www.memcpy.in
Number Of articles Posted
8
Number Of Messages Posted
23
Joined Date
0
   
Author
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

 
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android