CSharp Sending SMS
From SMS Wiki
Revision as of 08:21, 24 June 2008 by 207.81.129.251 (Talk)
The following sample code is running at SOAP Sampler
A complete set of examples can be downloaded from: SOAP API Usage Sample Code
Sending a Plain SMS
The basis of sending a message is to pass in the authentication token and signature along with the message parameters. A SoapException will be thrown if a serious error occurs and an SMS result object will be returned in less serious conditions.
Default.aspx.cs
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using com.upsidewireless.api.sms; using System.Web.Services.Protocols; public partial class SMS_Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void sendButton_Click(object sender, EventArgs e) { SMS sms = new SMS(); string token = ""; if (Session["token"] != null) { token = (string)Session["token"]; } string signature = ""; if (Session["signature"] != null) { signature = (string)Session["signature"]; } // If sending URLs special characters like "-", "+", "/" MUST be replaced with their safe equivalents (for example %2f). string recipient = recipientTextBox.Text; string message = messageTextBox.Text; SmsEncoding encoding = SmsEncoding.Seven; switch (encodingDropDownList.SelectedValue) { case "Seven": encoding = SmsEncoding.Seven; break; case "Sixteen": encoding = SmsEncoding.Sixteen; break; } try { SMSSendResult result = sms.Send_Plain(token, signature, recipient, message, encoding); if (result.isOk) { resultsMultiView.SetActiveView(resultSuccessView); trackingIdLabel.Text = result.trackingId; } else { resultsMultiView.SetActiveView(resultsSMSErrorView); blockedLabel.Text = result.BlockedReason; invalidCountryCodeCheckBox.Checked = result.invalidCountryCode; emptyMessageCheckBox.Checked = result.messageIsEmpty; tooManySplitsCheckBox.Checked = result.tooManyMessages; } } catch (SoapException error) { resultsMultiView.SetActiveView(resultsErrorView); errorLabel.Text = error.Detail.InnerText; } } }
Default.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="SMS_Default" Title="Send Plain SMS" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <table> <tr> <td align="right"> <asp:Label ID="recipientLabel" runat="server" Text="Recipient:"></asp:Label></td> <td> <asp:TextBox ID="recipientTextBox" runat="server"></asp:TextBox></td> </tr> <tr> <td valign="top"> <asp:Label ID="Label1" runat="server" Text="Message:"></asp:Label></td> <td> <asp:TextBox ID="messageTextBox" runat="server" Rows="6" TextMode="MultiLine"></asp:TextBox></td> </tr> <tr> <td valign="top"> <asp:Label ID="Label2" runat="server" Text="Encoding:"></asp:Label></td> <td> <asp:DropDownList ID="encodingDropDownList" runat="server"> <asp:ListItem Value="Seven">Seven (GSM7)</asp:ListItem> <asp:ListItem Value="Sixteen">Unicode</asp:ListItem> </asp:DropDownList></td> </tr> <tr> <td colspan="2" align="right"> <asp:Button ID="sendButton" runat="server" Text="Send" OnClick="sendButton_Click" /></td> </tr> </table> <asp:MultiView ID="resultsMultiView" runat="server"> <asp:View ID="resultSuccessView" runat="server"> Operation Successful<br /> Message Tracking ID:<asp:Label ID="trackingIdLabel" runat="server" Text="Label"></asp:Label></asp:View> <asp:View ID="resultsSMSErrorView" runat="server"> <div style="border: solid 1px #c00; padding: 20px; margin: 10px;"> The operation was not successful for the following reasons:<br /> <asp:Label ID="blockedLabel" runat="server" Text="Label" Font-Bold="True"></asp:Label><br /> <asp:CheckBox ID="invalidCountryCodeCheckBox" runat="server" Text="Invalid Country Code" /><br /> <asp:CheckBox ID="tooManySplitsCheckBox" runat="server" Text="Message too long" /><br /> <asp:CheckBox ID="emptyMessageCheckBox" runat="server" Text="Message is Empty" /></div> </asp:View> <asp:View ID="resultsErrorView" runat="server"> <div style="border: solid 1px #c00; padding: 20px; margin: 10px;"> <asp:Label ID="Label3" runat="server" Text="An error has occurred when calling this function:"></asp:Label> <asp:Label ID="errorLabel" runat="server" Text="Label" Font-Bold="True"></asp:Label></div> </asp:View> </asp:MultiView><br /> </asp:Content>