how to send sms from asp.net with c# using way2sms

C# Coding


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;


public partial class _Default : System.Web.UI.Page
{
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
    string mbno, mseg, ckuser, ckpass;
    private HttpWebRequest req;
    private CookieContainer cookieCntr;
    private string strNewValue;
    public static string responseee;
    private HttpWebResponse response;


    protected void Page_Load(object sender, EventArgs e)
    {
        //connect();
        if (!(IsPostBack))
        {          
            btnsend.Attributes.Add("onclick", "return Validate('" + txttono.ClientID + "','" + txtmsg.ClientID + "');");
           
        }
    }

   

   
    protected void btnsend_Click(object sender, EventArgs e)
    {        
    //    try
    //    {
    //        Session["id"] = txtuname.Text;
    //        Session["pw"] = txtpwd.Text;
    //        mbno = txttono.Text;
    //        mseg = txtmsg.Text;

    //        connect();
    //        sendSms(mbno, mseg);
    //        txttono.Text = "";
    //        txtmsg.Text = "";
    //    }
    //    catch (Exception ex)
    //    {
    //        lblError.Text = ex.Message;
    //        lblError.Visible = true;
    //    }
        try
        {

            send(txtuname.Text, txtpwd.Text, txtmsg.Text, txttono.Text);
            Response.Write("Message sent successfully....");
        }
        catch
        {
            Response.Write("Error Occured!!!");
        }
    }

    public void send(string uid, string password, string message, string no)
    {
        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://ubaid.tk/sms/sms.aspx?uid=" + uid + "&pwd=" + password + "&msg=" + message + "&phone=" + no + "&provider=way2sms");
        HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
        System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
        string responseString = respStreamReader.ReadToEnd();
        respStreamReader.Close();
        myResp.Close();
    }


    public void connect()
    {
        ckuser = Session["id"].ToString();
        ckpass = Session["pw"].ToString();

        try
        {
            this.req = (HttpWebRequest)WebRequest.Create("http://wwwd.way2sms.com/auth.cl");

            this.req.CookieContainer = new CookieContainer();
            this.req.AllowAutoRedirect = false;
            this.req.Method = "POST";
            this.req.ContentType = "application/x-www-form-urlencoded";
            this.strNewValue = "username=" + ckuser + "&password=" + ckpass;
            this.req.ContentLength = this.strNewValue.Length;
            StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
            writer.Write(this.strNewValue);
            writer.Close();
            this.response = (HttpWebResponse)this.req.GetResponse();
            this.cookieCntr = this.req.CookieContainer;
            this.response.Close();
            this.req = (HttpWebRequest)WebRequest.Create("http://wwwd.way2sms.com//jsp/InstantSMS.jsp?val=0");
            this.req.CookieContainer = this.cookieCntr;
            this.req.Method = "GET";
            this.response = (HttpWebResponse)this.req.GetResponse();
            responseee = new StreamReader(this.response.GetResponseStream()).ReadToEnd();
            int index = Regex.Match(responseee, "custf").Index;
            responseee = responseee.Substring(index, 0x12);
            responseee = responseee.Replace("\"", "").Replace(">", "").Trim();
            this.response.Close();           
            lblError.Text = "connected";
        }
        catch (Exception)
        {
            lblError.Text = "Error connecting to the server...";
            Session["error"] = "Error connecting to the server...";
        }
    }

    public void sendSms(string mbno, string mseg)
    {
        if ((mbno != "") && (mseg != ""))
        {
            try
            {
                this.req = (HttpWebRequest)WebRequest.Create("http://wwwd.way2sms.com//FirstServletsms?custid=");
                this.req.AllowAutoRedirect = false;
                this.req.CookieContainer = this.cookieCntr;
                this.req.Method = "POST";
                this.req.ContentType = "application/x-www-form-urlencoded";
                this.strNewValue = "custid=undefined&HiddenAction=instantsms&Action=" + responseee + "&login=&pass=&MobNo=" + this.mbno + "&textArea=" + this.mseg;

                string msg = this.mseg;
                string mbeno = this.mbno;

                this.req.ContentLength = this.strNewValue.Length;
                StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
                writer.Write(this.strNewValue);
                writer.Close();
                this.response = (HttpWebResponse)this.req.GetResponse();

                this.response.Close();
                lblError.Text = "Message Sent..... " + mbeno + ": " + msg;
            }
            catch (Exception)
            {
                lblError.Text = "Error Sending msg....check your connection...";
            }
        }
        else
        {
            lblError.Text = "Mob no or msg missing";
        }
    }

    //protected void btnclr_Click(object sender, EventArgs e)
    //{
    //    //txtuname.Text = "";
    //    //txtpwd.Text = "";
    //    //txttono.Text = "";
    //    //txtmsg.Text = "";
    //}
}
Previous
Next Post »
Thanks for your comment