ASP.NET Web Pages - Sending Text Using Textlocal

If you need to send a SMS message using ASP.NET, you can use textlocal SMS gateway to send the message. Textlocal is a UK-based company which allows you to use their API to send text messages. The following example will show you how to send a text in ASP.NET WebPages.

<%
  info = 1
  test = 0
  message = "Hello this is a test with a £5 note and an ampersand (&) symbol"
  message = Server.urlencode(message) 'encode special characters (e.g. £, & etc)
  from = "Jims Autos"
  address = "https://www.txtlocal.com/sendsmspost.php"
  uname = "youremailaddress"
  pword = "yourpassword"
  selectednums = "440000000000"
  url = address & "?uname=" & uname & "&pword=" & pword & "&message=" & message &_
      "&from=" & from & "&selectednums=" & selectednums & "&info=" & info &_
      "&test=" & test   
  set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
  xmlhttp.open "POST", url, false
  xmlhttp.send ""
  msg = xmlhttp.responseText
  response.write(msg)
  set xmlhttp = nothing
%>

The above code is the classic ASP.NET, which we can easily convert to asp.net web pages using the razor syntax.

Sponsored Links

C# Code

@{
 
var message="";
var from = "";
var uname = "";
var hash = "";
var selectednums="";
var url = "";
var address = "https://www.txtlocal.com/sendsmspost.php";
var info = 1;
var test = 1;
 
if (IsPost){
message  = Request["message"];
 
message = HttpUtility.UrlEncode(message); //encode special characters (e.g. £, & etc)
from = "";
uname = "";
hash = "";
selectednums = "";
 
url = address + "?uname=" + uname + "&hash=" + hash + "&message=" + message + "&from=" + from + "&selectednums=" + selectednums + "&info=" + info + "&test=" + test;   
Response.Redirect(url);
 
}
 
}

HTML Code

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <form method="post" >
        <input type="text" name="message" />
            
            <input type="submit"/>      
        
        </form>
    </body>
</html>

In this example we have done something different: we simply send the text when the user submits the form. Now the uname is your textlocal username, the hash is your hash which is available from your textlocal account. It is recommended you use hash instead of your password. The selectednums is the number you wish to send the text to, and this must begin with the country code. The test variable is either set to 1 or 0; 0 means it is not in test mode and your credit will go, while 1 means it is in test mode and your credit will not go.

The URL variable then simply joins everything up and you are redirected. The URL looks like this:

https://www.txtlocal.com/sendsmspost.php?uname=example@hotmail.co.uk&hash=yourhash&message=HELLO+WORLD+-+THIS+IS+A+TEXT+LOCAL+TEST!&from=yournamen&selectednums=yournumber&info=1&test=1

As you can see it just passes parameters.

If you wish to learn ASP.NET WebPages see the following:

https://www.thecodingguys.net/tutorials/asp/webpages

http://www.textlocal.com/developers/send-sms-api/