Popular Posts

Saturday, 21 December 2013

SENDING SMS THROUGH ASP.NET PROGRAMM

SENDING SMS OTP(ONE TIME  PASSWORD) TO USER MOBILE NO.


Hello friends this post explains the way where you can send the sms to any user mobile ,where i am sending or applying the concept on OTP to be send to registered user for his authentication.

In general, there are two ways to send SMS messages from a computer / PC to a mobile phone:
  1. Connect a mobile phone or GSM/GPRS modem to a computer / PC. Then use the computer / PC and AT commands to instruct the mobile phone or GSM/GPRS modem to send SMS messages.
  2. Connect the computer / PC to the SMS center (SMSC) or SMS gateway of a wireless carrier or SMS service provider. Then send SMS messages using a protocol / interface supported by the SMSC or SMS gateway.
HERE WE WILL DISCUSS THE SECOND WAY.TO REFER THE FIRST WAY I FOUND AN ARTICLE  ON CODE PROJECT:

THE DESIGN PAGE:

The design page will consists of user register form where he fills up the data and wait to recieve the sms code and need to enter the OTP and validation will be done.


THE CODE BEHIND:
TO SEND SMS:


this is the simple function created to send the OTP  to user mobile no:

   protected Int32 sendonetimepass(string username, string mobileno, string emailid)
    {
        try
        {
         

            Random _r = new Random();  //random number function to generate OTP

            int n = _r.Next(10000);


            System.IO.Stream Str = null;
            System.IO.StreamReader srRead = null;  //object of streamReader
            string PageContent = null;
            string strTmpContact = string.Empty;
            string message = "Dear User " + username + " ,Your One Time Password(OTP) is " + n + " ,please enter to confirm your identity/authenticity";
            if (n > 0)
            {
                string url = ("http://www.URPROVIDER.in/SendTestSMS/SendTESTMsg.php?uname=URREGISTERDNAME&pass=URPASSWORD&send=URREGISTRATIONNO&dest=91" + mobileno + "&msg=TITLE_OF_MESSAGE ") + message + "";


// the string Url consistis of  the the url string provided to u by the SERVICE PROVIDER TO SEND SMS.
// You can send SMS using ASP.net web application. First you hunt for good SMS provider API.    //API should be capable of sending long messages more than 160 characters.    //It works with http Get Method. Below is the code snippet to deal with an API.                


 System.Net.WebRequest req = System.Net.WebRequest.Create(url);
                System.Net.WebResponse resp = req.GetResponse();
                Str = resp.GetResponseStream();
                srRead = new System.IO.StreamReader(Str);
                // read all the text
                PageContent = srRead.ReadToEnd();

            }
            if ((srRead != null) & (Str != null))
            {
                srRead.Close();
                Str.Close();
            }
            return n;
        }

        catch (Exception ex)
        {
            return 0;

        }

    }



TO VALIDATE THE SMS ENTERED BY USER:

Once the sms is send successfully then you can save the sms of the user with his name and others details in the DB and can check when he enters.LIKE:



string query = "insert into userotpdetails values('" + txtUserName.Text + "','" + txtUserID.Text + "','" + txtPassword.Text + "','" + txtMobile.Text + "','" + txtEmailID.Text + "'," + otp + ",'" + currentdatetime + "')";
                DBAccess obj1 = new DBAccess();
                string result = obj1.OracleExecute(query);
                if (result == "Success")
                {
SYSTEM.OUT.PRINTLN("DETAILS SUBMMITED SUCCESFULLY");
               }

----------------------------------------------------------------------------------------------------------

No comments :

Post a Comment