ASP.NET - Code to send sms using webservice like way2sms
Asked By mahesh on 06-Apr-11 01:04 AM
Hello,
I want code to send sms using webservice like way2sms please help me out
Mihir Soni replied to mahesh on 06-Apr-11 01:14 AM
Hello,
the site which you have mentioned it doesn't provide any specific developer API to the developer. There are many third party web services available for way2sms but i would suggest you to use HttpwebRequest and HttpWebResponse class to use way2sms api :d
hope this helps you
thank you.
mahesh replied to Mihir Soni on 06-Apr-11 01:17 AM
hey hie...
can u provide me the code because I dont know how to use it....
Mihir Soni replied to mahesh on 06-Apr-11 01:26 AM

Hello,
here is code for you.
First of all declare following variable in your code file
string mbno, mseg, ckuser, ckpass;
private HttpWebRequest req;
private CookieContainer cookieCntr;
private string strNewValue;
public static string responseee;
private HttpWebResponse response;
Now write following code on your page load event
protected void Page_Load(object sender, EventArgs e)
{
ckuser = "Yourusername";
ckpass = "YourPassword";
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...";
}
}
Above code will connect your account to way2sms please chnage highlighted code with your username password
Now write following on btnMessageSend_Click event.
protected void btnSend_Click(object sender, EventArgs e)
{
mbno = txtTo.Text;//To mobile number
mseg = txtMessage.Text;//Message
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";
}
}
I have two textbox and one label on the page.
Hope this helps you
thank you
Anoop S replied to mahesh on 06-Apr-11 01:27 AM
You can use some webservice like
- For sending SMS to the world: http://www.webservicex.com/sendsmsworld.asmx?WSDL
- For sending SMS to India: http://www.webservicex.net/SendSMS.asmx?WSDL
for sending SMS with the help of GPRS(General Packet Radio
Service ).
refer this for more details and example
http://www.codeproject.com/KB/cpp/SendSmsThroughWS.aspx
Jitendra Faye replied to mahesh on 06-Apr-11 01:35 AM
For sending sms using C# code, first you have to configure your SMS Gateway,
here you have to usename and password.
plz follow below link for more details-
http://www.codeproject.com/KB/database/SMS_message_from_SQL.aspx
I hope this will help.
Nikhil Mahajan replied to mahesh on 06-Apr-11 01:44 AM
hi mahesh for sending sms...you need to buy sms gateway....
and when u buy sms gatway..they provide you api key..for different languages....
so that you can send sms by "get/post" method...
below is the code to send sms by post method
string username, password, To, udh, From, Text_msg, DrlUrl;
string amps = "&";
username = "xxxx";
password = "xxx";
To = txt_mobile.Text; //mobile no
udh = "xxxx";
From = "test"; //sender id
Text_msg = "ur msg here";
string main_string = "http://domain/sendsms?username=" + username + amps + "password=" + password + amps + "to=" + To.Trim() + amps + "udh=" + udh.Trim() + amps + "from=" + From.Trim() + amps + "message=" + Text_msg.Trim() + amps;
Uri cmd12 = new Uri(main_string);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(cmd12);
request.Method = "POST";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
mahesh replied to Mihir Soni on 06-Apr-11 02:02 AM
hi,
on this line StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
i am getting error The name 'Encoding' does not exist in the current context
I have added following headers
using System.Web.Services;
using System.Net;
using System.IO;
Mihir Soni replied to mahesh on 06-Apr-11 02:04 AM
Hello,
Here is download link of simple website which is described into the code
download it
http://www.mediafire.com/?dv0kmznloin
Nikhil Mahajan replied to mahesh on 06-Apr-11 02:11 AM
you are getting this error message because you didnt import the namespace
im port this namespace to use Encoding Class
mahesh replied to Mihir Soni on 06-Apr-11 02:33 AM
Hi,
I have downloaded it the code is getting compiled but still m not receiving msg on mobile device.
mahesh replied to Nikhil Mahajan on 06-Apr-11 02:36 AM
hi,
What is sms gateway and how much will it cost
Nikhil Mahajan replied to mahesh on 06-Apr-11 02:42 AM
in layman's language SMS gateway
An SMS gateway is a way of sending a text message with or without using a mobile (cell) phone. Specifically, it is a device or service offering SMS transit by either transforming messages to mobile network traffic from other media or by allowing transmission or receipt of SMS messages with or without the use of a mobile phone. Typical use of a gateway would be to forward simple email to a mobile phone recipient. It can also be useful in developing web applications that we can interact with via SMS (Short Messaging Service).
and in developer lang..
SMS gateway..
is the api through which you can send sms by your application....
there are different types of sms like "normal,priority sms,marketing sms....
so that depends on you.....that which one to buy........
there are many sites from where you can buy sms gateway...of range 3paise-10/20 paise depends on priority of message
dipa ahuja replied to mahesh on 06-Apr-11 07:40 AM
Download the new dll for way2sms :
and this is the code :
CookieContainer cookie = SMSClientLib.Login.Connect("mobileno.", "Password", out status);
string[] siteParameters = SMSClientLib.Login.GetSiteParameters(cookie);
string messgeSentResult = SendSMS.Send_Processing("RecMobile", "Message!", cookie, siteParameters);
Its Working...!!
Hope this will help you
ashok replied to mahesh on 06-Jun-11 06:37 AM
Thank you.
This code is working fine....
sagar replied to mahesh on 16-Jul-11 06:21 AM
hey man this code did not work is there any errors
Karthik K replied to mahesh on 27-Jul-11 10:23 AM
Hello,
The dll does work,
With a small change,
string status;
CookieContainer cookie = SMSClientLib.Login.Connect("9841568894", "1234", out status);
string[] siteParameters = SMSClientLib.Login.GetSiteParameters(cookie);
string messgeSentResult = SMSClientLib.SendSMS.Send_Processing("9841568894", "Message!", cookie, siteParameters);
I have highlighted the changes made
Good Dll..!!
Prasana replied to mahesh on 01-Aug-11 11:40 AM
hai...... sms to send using asp.net through webservices way2sms.com...... doesnt work.......
it came.. tat sms send successfully., but no sms recieved in my mobile num....
wat to do...... please give me some soln......
i just followed the code u gave
Prasana replied to dipa ahuja on 01-Aug-11 11:50 AM
hi........
May i know where i ve to paste tat .dll file..... please do reply......
i need it dude........
Arun replied to mahesh on 03-Aug-11 04:34 AM
Please could you tell how to use the dll in the .net code. It seems it can't be added as a reference in .net code with the error "the dll doenot seems to be a valid .net/com component". Any poinetrs would be appreciated
jismy replied to mahesh on 17-Sep-11 03:07 AM
Hi, I tried Your code Its showing one error that RegEx is not exists ,Can U pls tell me what is mean by REgex in the folloying code
int index = Regex.Match(responseee, "custf").Index;
bhupendra replied to mahesh on 23-Sep-11 01:03 PM
i want send sms using web services like way2sms and 160by2 .............. immediately i have require source code ...........so kindly help me..............my email id singhbhupendra54@yahoo.com
Karthik Durairaj replied to Karthik K on 31-Oct-11 06:57 AM
Hi,
The code works fine but i didnt receive any message.
swapnil goew replied to Karthik Durairaj on 19-Nov-11 01:57 PM
Way2sms webservice give error 404 not found
Eswar Rao replied to Karthik K on 13-Jan-12 06:26 AM
Hey SmsClientLibrary not working
sathish suresh replied to Mihir Soni on 26-Jan-12 06:14 AM
hai........ could u upload the file or send me the link where i have to download it....... i tried but its not working........ r u could u able to provide me code in vb.net please help thanks........... with regards sathish
Dr.Nick S replied to sathish suresh on 01-Feb-12 08:50 PM

- Imports System.Net
- Imports System.IO
-
- Partial Class demo
- Inherits System.Web.UI.Page
-
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- SendSMS("18479790553", "ToNumber", "Do Not Reply: You have an appointment on 1-20-2012 at 3:00pm with Dr. McAndrews at the Carson Office. Please arrive 15 minutes early. ")
- End Sub
-
- Sub SendSMS(ByVal FromNumber As String, ByVal ToNumber As String, ByVal SMS As String)
- Response.ContentType = "text/xml; charset=utf-8"
- Dim url As String = "https://www.sendandreceivesms.com/api/"
- Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
- request.Method = "POST"
- Dim postData As String = "FromNumber=" & FromNumber
- postData += "&"
- postData += "ToNumber=" & ToNumber
- postData += "&"
- postData += "SMS=" & SMS
- Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
- request.ContentType = "application/x-www-form-urlencoded"
- request.Headers.Add("APIToken", "GoOu9H0h9G")
- request.ContentLength = byteArray.Length
- Dim dataStream As Stream = request.GetRequestStream()
- dataStream.Write(byteArray, 0, byteArray.Length)
- dataStream.Close()
- Dim _webresponse As WebResponse = request.GetResponse()
- dataStream = _webresponse.GetResponseStream()
- Dim reader As New StreamReader(dataStream)
- Dim responseFromServer As String = reader.ReadToEnd()
- Response.Write(responseFromServer)
- reader.Close()
- dataStream.Close()
-
- End Sub
- End Class
Chaithanya Kumar replied to mahesh on 10-Feb-12 11:54 AM
Sunny Tillu replied to Mihir Soni on 18-Feb-12 11:14 AM
vinay kumar replied to Nikhil Mahajan on 21-Feb-12 04:02 AM
vinay kumar replied to Nikhil Mahajan on 21-Feb-12 04:02 AM
Sreelesh Mp replied to jismy on 03-Mar-12 01:25 AM
import the namespace System.Text.RegularExpressions;
Sreelesh Mp replied to jismy on 03-Mar-12 01:26 AM
import namespace using System.Text.RegularExpressions;
9925584610
Mahesh Baria replied to Dr.Nick S on 07-Mar-12 12:34 PM
prashant mohite replied to Dr.Nick S on 09-Mar-12 01:44 AM
When i paste this code in DEMO.ASPX then shows this error.....
I have already add SMSClientLibrary.dll File in BIN folder ( Reference folder )
XML Parsing Error: junk after document element
Location: http://localhost:2610/WebSite7/Demo.aspx
Line Number 3, Column 1:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
^
Please help me
Rgd.
Prashant
prashant mohite replied to Dr.Nick S on 09-Mar-12 02:07 AM
copy and paste whole the code in Demo.aspx but shows Below error..........
XML Parsing Error: junk after document element
Location: http://localhost:1706/WebSite6/Demo.aspx
Line Number 3, Column 1:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
^
Any service start for this code or...........
from
Prashant
Dr.Nick S replied to prashant mohite on 09-Mar-12 02:12 AM
That is because your demo.aspx has some html tags that is usually auto generated by visual studio.
The code behind returns and XML response.
The xml response interferes with the HTML tags on the page.
Remove all HTML tags and just leave the page directive in demo.aspx and you will see the XML response.
Which country do you want to primarily use it for?
prashant mohite replied to Dr.Nick S on 11-Mar-12 01:20 AM
is this write code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Demo.aspx.vb" Inherits="Demo" %>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
-----------------------------------------------------------------
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Demo.aspx.vb" Inherits="Demo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
I TRIED BOTH THE METHOD MY GIVE ERROR I AM USING THIS SERVICE FROM INDIA.
Dr.Nick S replied to prashant mohite on 11-Mar-12 04:09 AM
Take out this HTML from demo.aspx
<HTML>
<head run at="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" run at="server">
<div>
</div>
</form>
</body>
</HTML>
Dr.Nick S replied to prashant mohite on 11-Mar-12 04:09 AM
Download the sample app
http://www.sendandreceivesms.com/sendsmsusingvb.net.zip?dl=1
Dr.Nick S replied to prashant mohite on 11-Mar-12 04:12 AM
I am sorry we cannot edit these posts
Delete the following markup from demo.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Dr.Nick S replied to Dr.Nick S on 11-Mar-12 04:12 AM
So your Demo.aspx Should only be
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Demo.aspx.vb" Inherits="Demo" %>
raja azhagu replied to Dr.Nick S on 13-Mar-12 07:15 AM
hai friends,......i used this code am getting error at pnlsend.visible=true;...i.e..name 'pnlsend' does not exists in the current context..help me plzz
Mahesh Bajjuri replied to Chaithanya Kumar on 18-Apr-12 02:00 AM
hi
when iam trying to use this code , i got an error when connecting to way2sms at this line
-----> this.response = (HttpWebResponse)this.req.GetResponse();
the error is :
The remote server returned an error: (404) Not Found.
can anyone help me to resoolve this error.
Thanks in advance
Mahesh Bajjuri replied to Mihir Soni on 18-Apr-12 02:25 AM
hi
when iam trying to use this code , i got an error when connecting to way2sms at this line
-----> this.response = (HttpWebResponse)this.req.GetResponse();
the error is :
The remote server returned an error: (404) Not Found.
can anyone help me to resoolve this error.
Thanks in advance
lalit sharam replied to Mihir Soni on 05-May-12 03:26 AM
hey this code work on vb tell me ....
anil babu replied to Mihir Soni on 17-Sep-12 01:30 AM
i am geeting always ::::::::Error connecting to the server..."
What is this error here ::::: :pnlsend.Visible = true;
sathish kumar replied to Mahesh Bajjuri on 04-Apr-13 08:42 AM
when iam trying to use this code , i got an error when connecting to way2sms at this line
-----> this.response = (HttpWebResponse)this.req.GetResponse();
the error is :
The remote server returned an error: (404) Not Found.
Same problem to me..
what wrong in code