Wednesday, September 21

ASP.net code to send a single SMS through an ASP.net web page with C#.

File: SMSAdapter.cs

using System;
using System.Net;
using System.IO;

namespace c4Sms
{
 public class SMSAdapter
 {
  public SMSAdapter()
  {
  }

  private const string referalUrl = "https://api.c4sms.com/";

  private string username;
  public string Username
  {
   get { return username; }
   set { username = value; }
  }

  private string password;
  public string Password
  {
   get { return password; }
   set { password = value; }
  }

  public SMSResponse Send(SMSMessage message)
  {

   WebClient wc = new WebClient();

   wc.QueryString.Add("username", Username);
   wc.QueryString.Add("password", Password);
   wc.QueryString.Add("sendto", message.recipient);
   wc.QueryString.Add("message", message.message);
   if (!message.from == string.Empty) wc.QueryString.Add("from", message.from);

   Stream responseStream = wc.OpenRead(referalUrl);
   StreamReader sr = new StreamReader(responseStream);
   string responseString = sr.ReadToEnd();

   SMSResponse response = new SMSResponse();

   if (responseString == "Null") response.invalidCredentials = true;

   if (!response.invalidCredentials) response.success = (decimal.Parse(responseString) > 0);

   response.response = responseString;

   sr.Close();
   responseStream.Close();
   return response;
  }
 }

 public struct SMSMessage
 {
  public string recipient;
  public string message;
  public string from = string.Empty;
 }

 public struct SMSResponse
 {
  public bool success;
  public bool invalidCredentials;
  public string response;
 }
}
File: YourFile.aspx



SMSAdapter adapter = new SMSAdapter();

adapter.Username = "YourUsername";

adapter.Password = "YourPassword";

SMSMessage message = new SMSMessage();

message.recipient = "44123456789";

message.message = "Example message";

//Optional

message.from = "44123456789"

SMSResponse response = adapter.Send(message);

SMSResponse contains 3 members, all are self explanatory:

public struct SMSResponse

{

           public bool success;

           public bool invalidCredentials;

           public string response;

}
Below is the ASP (Active Server Page) code to send a single SMS through an ASP web page.
in Vb:-



<%
 Option Explicit

 '****************
 'Replace values with your C4 username and password
 CONST c4USERNAME = "Your C4 Username Here"
 CONST c4PASSWORD = "Your C4 Password Here"
 '****************
 Dim strReturnValue

 'Function to send the SMS message
 Function sendSMS(strSendToNumber,strMessage,strFromNumber)

  'Create new variables
  Dim strQueryString  'Holds data to post to C4 website
  Dim objHttp    'The XMLHTTP object to make the post request
  Dim strResponseTxt  'The response from the C4 server

  'Build the query string
  strQueryString = "username=" & c4USERNAME & "&password=" & c4PASSWORD & "&sendto=" & strSendToNumber & "&message=" & strMessage
  if strFromNumber <> "" then
   strQueryString = strQueryString & "&from=" & strFromNumber
  end if

  'Send the message
  set objHttp = Server.CreateObject("Microsoft.XMLHTTP")
  objHttp.open "POST", "https://api.c4sms.com/?" & strQueryString, false
  objHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
  objHttp.Send
   strResponseTxt = trim(objHTTP.responseText)
  Set objHTTP = nothing

  'Return the value from this function
  sendSMS = strResponseTxt

 End function

 'This line of code actually send the SMS message
 strReturnValue = sendSMS("44789123456","This is my message","44789123456")

 'Determine the messages status
 Select case strReturnValue

  Case "0"
   Response.write("Error!  Not enough credits in your account, or there was a routing error.")
  Case "Null"
   Response.write("Error!  C4 Password and Username are invalid.  Please check.")
  Case else
   if isnumeric(strReturnValue) then
    Response.write("Message was sent at a cost of " & strReturnValue & " credits")
   else
    Response.write("Unspecified Error Occured")
   end if
 End select
%>
Below is the ASP (Active Server Page) code to send multiple SMS with a single click through an ASP web page.
Vb script:-





<%
 Option Explicit

 Dim strAction
 Dim arrSplitNumbers
 Dim strReturnValue
 Dim strSentFrom
 Dim strSingleNumber
 Dim i
 Dim strTxtMessage
 Dim lngCreditCostCounter
 Dim lngFailedCounter
 Dim lngSentCounter

 '****************
 'Replace values with your C4 username and password
 CONST c4USERNAME = "Your C4 Username Here"
 CONST c4PASSWORD = "Your C4 Password Here"
 '****************

 'Get the action of this page
 strAction = request.querystring("action")

 'Function to send the SMS message
 Function sendSMS(strSendToNumber,strMessage,strFromNumber)

  'Create new variables
  Dim strQueryString  'Holds data to post to C4 website
  Dim objHttp    'The XMLHTTP object to make the post request
  Dim strResponseTxt  'The response from the C4 server

  'Build the query string
  strQueryString = "username=" & c4USERNAME & "&password=" & c4PASSWORD & "&sendto=" & strSendToNumber & "&message=" & strMessage
  if strFromNumber <> "" then
   strQueryString = strQueryString & "&from=" & strFromNumber
  end if

  'Send the message
  set objHttp = Server.CreateObject("Microsoft.XMLHTTP")
  objHttp.open "POST", "http://www.c4sms.com/httpsend.asp?" & strQueryString, false
  objHttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
  objHttp.Send
   strResponseTxt = trim(objHTTP.responseText)
  Set objHTTP = nothing

  'Return the value from this function
  sendSMS = strResponseTxt

 End function

 'If we are sending the message
 if strAction = "send" then

  'Get the numbers entered on the form
  arrSplitNumbers = split(Request.form("numbers"),",")

  'Get the from number
  strSentFrom = Request.form("from")

  'Get the message
  strTxtMessage = left(Request.form("message"),160)

  'Initialise counters
  lngCreditCostCounter = 0
  lngFailedCounter= 0
  lngSentCounter = 0

  'Loop through the numbers
  for i=0 to Ubound(arrSplitNumbers)

   'Take a single number
        strSingleNumber = trim(arrSplitNumbers(i))

   'Send the SMS message
   strReturnValue = sendSMS(strSingleNumber,strTxtMessage,strSentFrom)

   'Determine the messages status
   Select case strReturnValue

    Case "0"
     lngFailedCounter = lngFailedCounter + 1
    Case "Null"
     lngFailedCounter = lngFailedCounter + 1
    Case else
     if isnumeric(strReturnValue) then
      lngCreditCostCounter = lngCreditCostCounter + cdbl(strReturnValue)
      lngSentCounter = lngSentCounter + 1
     else
      lngFailedCounter = lngFailedCounter + 1
     end if
   End select

  next

 end if

 if strAction = "send" then
%>
Message send attempt was made<BR><BR>

<font color="#ff0000"><B><%=lngFailedCounter%></b> messages failed to send</font><BR>
<font color="#008000"><B><%=lngSentCounter%></b> messages sucessfully sent</font><BR>
<B><%=lngCreditCostCounter%> total credit cost<BR><BR>

<a href="test.asp">Send another</a>
<%else%>
<form action="test.asp?action=send" method="post">
<B>Numbers</b><BR>
Enter the mobile numbers to send this message to (seperate with commas)<BR>
<input type="text" name="numbers" size="40"><BR><BR>

<B>Message</b><BR>
The message to send to these numbers (160 chars max)<BR>
<textarea name="message" rows="3" cols="40"></textarea><BR><BR>

<B>From</b><BR>
The number the message will appear from (optional)<BR>
<input type="text" name="from" size="40"><BR><BR>
<input type="submit" value="Send Them"></form>

</form>
<%end if%>




No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

.

ShibashishMnty
shibashish mohanty