How to create our own SMS gateway using ASP.net C#
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 link to remember Mr Shefeek MJ 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 under 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 . 


See All Responses Below...
Author
Message Rating
Posted on:
Please Login to Post Your Comments
Name (Max. 100 characters)
Please post your comments here
Select Language
Comments
Attach File(Max. Size: 2 MB)
A few simple rules when posting your Comments,
  1. Please post only answers relevant to the topic of discussion.
  2. Please dont misuse this site or do not be abusive, offensive, inappropriate,harass anyone on the boards or post ads or spam. Doing so will delete your inappropriate messages and will block or delete your account on this site. 

 
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android