ASP.NET - How to Send sms - Asked By msakt on 11-Nov-11 04:51 AM

how to send sms in asp.net
Anoop S replied to msakt on 11-Nov-11 06:42 AM
Are you from India? then easiest way is use way2sms, just register there and refer this code


 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)
    {
        if (Session["id"] == null && Session["pw"] == null)
        {
            Server.Transfer("smslogin.aspx");
        }
        connect();
        
    }

 protected void btnSend_Click(object sender, EventArgs e)
    {       
        try
        {
            mbno = txtTo.Text;
            mseg = txtMessage.Text;
           
            sendSms(mbno, mseg);
            txtTo.Text = "";
            txtMessage.Text = "";
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
            lblError.Visible = true;
        }
    }
    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();
          
            pnlsend.Visible = true;
            lblErrormsg.Text = "connected";
        }
        catch (Exception)
        {
            lblErrormsg.Text = "Error connecting to the server...";
            Session["error"] = "Error connecting to the server...";
            Server.Transfer("smslogin.aspx");


        }
    }
    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();
                lblErrormsg.Text = "Message Sent..... " + mbeno + ": " + msg;
            }
            catch (Exception)
            {
                lblErrormsg.Text="Error Sending msg....check your connection...";
            }
        }
        else
        {
            lblErrormsg.Text = "Mob no or msg missing";
        }
    }

refer this for m,ore details and full code
http://www.dotnetspark.com/kb/1978-send-sms-from-asp-net-using-way2sms-account.aspx
Kirtan Patel replied to msakt on 11-Nov-11 07:27 AM
you can use way2sms to send sms using asp.net what u need is way2sms username and password 

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();
      
    pnlsend.Visible = true;
    lblErrormsg.Text = "connected";
  }
  catch (Exception)
  {
    lblErrormsg.Text = "Error connecting to the server...";
    Session["error"] = "Error connecting to the server...";
    Server.Transfer("smslogin.aspx");
 
 
  }
}
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();
      lblErrormsg.Text = "Message Sent..... " + mbeno + ": " + msg;
    }
    catch (Exception)
    {
      lblErrormsg.Text="Error Sending msg....check your connection...";
    }
  }
  else
  {
    lblErrormsg.Text = "Mob no or msg missing";
  }
}