How to send SMS using ASP.NET through HTTP


/ Published in: ASP
Save to your folder(s)

Hello Guys,

This short ASP.NET code snippet is intended to provide you a brief review on how to add SMS functionality to your website. You will see, this is a very simple but smart solution. This ASP.NET application is able to send messages by using HTTP requests towards the SMS gateway that sends the SMSs to the destination telephone via a GSM modem or an IP SMS connection.

Let’s take a quick look at the software requirements that are essentially needed for tjis solution. In order to send SMS messages from your ASP.NET application, you need a development platform, e.b Visual Studio, of course, .NET Framework 4.0, Internet Information Services (IIS) and an SMS gateway (I used Ozeki NG – http://www.ozekisms.com). You also need a GSM modem attached to your PC or an IP SMS connection to be able to send messages.

Okay ans now let’s use the code snippet! Copy the content of smssend.aspx and smssend.aspx.cs into the main directory of the IIS server - C:\Inetpub\wwwroot\ directory (). Configure the fixed data in the smssend.aspx.cs file (the IP address and port number of the SMS gateway, username, password). Launch your SMS gateway Server. Start a web browser and enter this URL: http://127.0.0.1/smssend.aspx - where 127.0.0.1 means that the smssend.aspx and smssend.aspx.cs files can be found on the same computer on which the browser has been opened). Fill in the required fields, and then click the Send button. It’s as easy as that!

Happy coding! :)


Copy this code and paste it in your HTML
  1. //smssend.sapx
  2.  
  3. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="smssend.aspx.cs" Inherits="_Default" validateRequest="false" %>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6.  
  7. <html xmlns="http://www.w3.org/1999/xhtml" >
  8. <head runat="server">
  9. <title>How to send SMS using ASP.NET through HTTP</title>
  10. </head>
  11. <body>
  12. <center>
  13. <form id="smsdata" runat="server">
  14. <asp:Table id="smstable" runat="server" style="text-align:left; border-width:thin; border-color:Silver;" BorderStyle="Solid">
  15. <asp:TableRow>
  16. <asp:TableCell ColumnSpan="2">
  17. <b>Compose a message:</b>
  18. <br />
  19. <br />
  20. </asp:TableCell>
  21. </asp:TableRow>
  22. <asp:TableRow>
  23. <asp:TableCell HorizontalAlign="Left" VerticalAlign="Top">
  24. <asp:Label ID="labelRecipient" runat="server" Text="Recipient: "></asp:Label>
  25. </asp:TableCell>
  26. <asp:TableCell>
  27. <asp:TextBox ID="textboxRecipient" runat="server"></asp:TextBox>
  28. </asp:TableCell>
  29. </asp:TableRow>
  30. <asp:TableRow>
  31. <asp:TableCell HorizontalAlign="Left" VerticalAlign="Top">
  32. <asp:Label ID="labelMessage" runat="server" Text="Message Text: "></asp:Label>
  33. </asp:TableCell>
  34. <asp:TableCell>
  35. <asp:TextBox ID="textboxMessage" runat="server" TextMode="MultiLine"></asp:TextBox>
  36. </asp:TableCell>
  37. </asp:TableRow>
  38. <asp:TableRow>
  39. <asp:TableCell ColumnSpan="2" HorizontalAlign="Center">
  40. <asp:Button ID="buttonSend" runat="server" Text="Send Message" OnClick="buttonSendOnClick" />
  41. </asp:TableCell>
  42. </asp:TableRow>
  43. <asp:TableRow>
  44. <asp:TableCell ColumnSpan="2" HorizontalAlign="Center">
  45. <asp:TextBox ID="textboxError" runat="server" BorderStyle="None" TextMode="MultiLine"></asp:TextBox>
  46. </asp:TableCell>
  47. </asp:TableRow>
  48. </asp:Table>
  49. </form>
  50. </center>
  51. </body>
  52. </html>
  53.  
  54. //smssend.aspx.cs
  55.  
  56. using System;
  57. using System.Data;
  58. using System.Configuration;
  59. using System.Web;
  60. using System.Web.Security;
  61. using System.Web.UI;
  62. using System.Web.UI.WebControls;
  63. using System.Web.UI.WebControls.WebParts;
  64. using System.Web.UI.HtmlControls;
  65. using System.Net;
  66. using System.Text.RegularExpressions;
  67.  
  68. public partial class _Default : System.Web.UI.Page
  69. {
  70.  
  71.  
  72. protected void Page_Load(object sender, EventArgs e)
  73. {
  74. textboxRecipient.Width = 400;
  75. textboxMessage.Width = 450;
  76. textboxMessage.Rows = 10;
  77. textboxError.Width = 400;
  78. textboxError.Rows = 5;
  79.  
  80. textboxError.ForeColor = System.Drawing.Color.Red;
  81. textboxError.Visible = false;
  82. textboxError.Text = "";
  83.  
  84. if (!Page.IsPostBack)
  85. {
  86. textboxRecipient.Text = "+441234567";
  87. textboxMessage.Text = "Test message!";
  88. }
  89. }
  90.  
  91. protected void buttonSendOnClick(object sender, EventArgs e)
  92. {
  93. //are required fields filled in:
  94. if (textboxRecipient.Text == "")
  95. {
  96. textboxError.Text += "Recipient(s) field must not be empty!\n";
  97. textboxError.Visible = true;
  98. return;
  99. }
  100.  
  101. //we creating the necessary URL string:
  102. string ozSURL = "http://127.0.0.1"; //where the SMS Gateway is running
  103. string ozSPort = "9501"; //port number where the SMS Gateway is listening
  104. string ozUser = HttpUtility.UrlEncode("admin"); //username for successful login
  105. string ozPassw = HttpUtility.UrlEncode("abc123"); //user's password
  106. string ozMessageType = "SMS:TEXT"; //type of message
  107. string ozRecipients = HttpUtility.UrlEncode(textboxRecipient.Text); //who will get the message
  108. string ozMessageData = HttpUtility.UrlEncode(textboxMessage.Text); //body of message
  109.  
  110. string createdURL = ozSURL + ":" + ozSPort + "/httpapi" +
  111. "?action=sendMessage" +
  112. "&username=" + ozUser +
  113. "&password=" + ozPassw +
  114. "&messageType=" + ozMessageType +
  115. "&recipient=" + ozRecipients +
  116. "&messageData=" + ozMessageData;
  117.  
  118. try
  119. {
  120. //Create the request and send data to the SMS Gateway Server by HTTP connection
  121. HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(createdURL);
  122.  
  123. //Get response from the SMS Gateway Server and read the answer
  124. HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
  125. System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
  126. string responseString = respStreamReader.ReadToEnd();
  127. respStreamReader.Close();
  128. myResp.Close();
  129.  
  130. //inform the user
  131. textboxError.Text = responseString;
  132. textboxError.Visible = true;
  133. }
  134. catch (Exception)
  135. {
  136. //if sending request or getting response is not successful the SMS Gateway Server may do not run
  137. textboxError.Text = "The SMS Gateway Server is not running!";
  138. textboxError.Visible = true;
  139. }
  140.  
  141. }
  142. }

URL: http://www.ozekisms.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.