My Professional Profile

I am Johnson Augustine Sr.Software Engineer and System Architect. I have 10 Yrs of hands on expertise in ASP.net MVC 5 , Angular 5 , C# MVC Razor, WPF MVVM , Android , IOS Swift 3 , IOS11 MSSQL,MySQL Database,,PHP,C/C++/Visual C++/G++/QT++,Com,DirectX,Open CV,EMGU CV , embedded System Development , [Raspberry PI]. ,html,Javascript,Jquery,Ajax.CSS , Networking ,Cyber security, Ethical Hacking You can see my professional profile at http://linkedin.com/in/johnsontaugustine Email : johnsonaugustine@live.com

Friday, 14 December 2012

How to send sms from ASP.net C# code


SMS Gateway

[This project was done for the completion of my  BCA Degree in 2008-2009 and past two years it was in the industry for commercial use. And i thank Mr Shefeek MJ for his innovative support , Now he is working for wipro technologies as a project engineer - Author - Johnson TA ]

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.To send SMS from your pc you should need the following components .

1) A PC with USB or Parallel ports enabled.
2) A GSM Modem [ A GSM Mobile with Data cable or Bluetooth connected ] support AT Commands
3) ASP.net [.net frame 3.5 installed ]

What we are doing is , just 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 next post].



 //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
            //2 .GeneratePdu(string receiver, string msg) ----> Generates SMS in PDU format using the other functions
            //3.MessageToHex(string message)------>Function to convert message to Hexadecimal 8-bit octets
            //4.getOctet(string receiver)-------->Function to Encrypt Receiver and Message Centre Number(SemiOctet)
            //5.SendToSMSEngine(string pdu,int length)----->Exit point from this class.It passes the final SMS in the PDU format to the SMS Engine



The just use the bellow codes to send SMS .

============================================

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;
        }
    }
}


==========================================

To send SMS using API just use an external sms gateway . See this
This Code is fully copy righted to Mr.Johnson Augustine , You people can use this under satisfying all regulations and rules from TRAI .

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 . 

This code is for study purpose only . 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.