How to send SMS / OTP SMSin asp.net using c# Easy Way To send - OmIndia

Teach To India

Friday, August 04, 2017

How to send SMS / OTP SMSin asp.net using c# Easy Way To send

How to send SMS /OTP in asp.net using c#

     Public void SendSms()
        {
            try
            {
                string username = "";
                string password = "";
                string sender = "test";
                string domian = "";
                string Heading1 = "Welcome To your website";
                string Heading2 = string.Concat("Dear ", txtName.Text, ",", "Your Confirmation Is:",                                                       txtcode.Text);
                string end = "Enter this code in the registration form. Thanking You";
                string Msg = string.Concat(Heading1, ",", Heading2, ",", end);
                string pn = txtMobile.Text;
                string result = apicall("http://" + domian + "/WebserviceSMS.aspx?username=" +                           username + "&password=" + password + "&sender=" + sender + "&to=" + pn +                               "&message=" + Msg);       
            }
            catch
            {
            }       
        }

        public string apicall(string url)
        {
            HttpWebRequest httpreq = (HttpWebRequest)WebRequest.Create(url);
            try
            {
                HttpWebResponse httpres = (HttpWebResponse)httpreq.GetResponse();
                StreamReader sr = new StreamReader(httpres.GetResponseStream());
                string results = sr.ReadToEnd();
                sr.Close();
                return results;
            }
            catch
            {
                return "0";
            }
        }








Another Code Snip -

using System;

using System.Data;

using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text.RegularExpressions;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        textboxRecipient.Width = 400;
        textboxMessage.Width = 450;
        textboxMessage.Rows = 10;
        textboxError.Width = 400;
        textboxError.Rows = 5;
        textboxError.ForeColor = System.Drawing.Color.Red;
        textboxError.Visible = false;
        textboxError.Text = "";
        if (!Page.IsPostBack)
        {
            textboxRecipient.Text = "+441234567";
            textboxMessage.Text = "Test message!";
        }
    }
    protected void buttonSendOnClick(object sender, EventArgs e)
    {
        //are required fields filled in:
        if (textboxRecipient.Text == "")
        {
            textboxError.Text += "Recipient(s) field must not be empty!\n";
            textboxError.Visible = true;
            return;
        }
        //we creating the necessary URL string:
        string ozSURL = "http://127.0.0.1"; //where the SMS Gateway is running
        string ozSPort = "9501"; //port number where the SMS Gateway is listening
        string ozUser = HttpUtility.UrlEncode("admin"); //username for successful login
        string ozPassw = HttpUtility.UrlEncode("abc123"); //user's password
        string ozMessageType = "SMS:TEXT"; //type of message
        string ozRecipients = HttpUtility.UrlEncode(textboxRecipient.Text); //who will get the message
        string ozMessageData = HttpUtility.UrlEncode(textboxMessage.Text); //body of message
        string createdURL = ozSURL + ":" + ozSPort + "/httpapi" +
            "?action=sendMessage" +
            "&username=" + ozUser +
            "&password=" + ozPassw +
            "&messageType=" + ozMessageType +
            "&recipient=" + ozRecipients +
            "&messageData=" + ozMessageData;
        try
        {
            //Create the request and send data to the SMS Gateway Server by HTTP connection
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(createdURL);
            //Get response from the SMS Gateway Server and read the answer
            HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
            System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
            string responseString = respStreamReader.ReadToEnd();
            respStreamReader.Close();
            myResp.Close();
            //inform the user
            textboxError.Text = responseString;
            textboxError.Visible = true;
        }
        catch (Exception)
        {
            //if sending request or getting response is not successful the SMS Gateway Server may do not run
            textboxError.Text = "The SMS Gateway Server is not running!";
            textboxError.Visible = true;
        }
    }
}

2 comments:

  1. yeah!! Two Factor Authentication stand as the one stop solution for all security concerns related to digitization. Under such prophecy, this service is the tool for ensuring round tipped securities while performing transactions, signing up/signing in events and crucial digital activities performed by individuals.

    ReplyDelete

Comments

Popular