Wednesday, August 19, 2009

Sending SMS from .Net application

hi,

.Net has the support for the mobile device. Using the Application we can send SMS to the mobile device, for this i am using the 2 web service in the application that are com.webservicex.www & net.webservicex.www. We simply need to create an interface from which we can supply the mobile no and the message. Using this service not only we can send message to India but we also can send the message to international numbers. You just need to add the Web reference in ypur application for this 2 web services.

please refer this code for .CS page.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace SmsTest
{
///
/// Summary description for WebForm1.
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Send;
protected System.Web.UI.WebControls.TextBox txtMobileNo;
protected System.Web.UI.WebControls.TextBox txtEmailId;
protected System.Web.UI.WebControls.TextBox txtCountryCode;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.RadioButtonList rdoType;
protected System.Web.UI.WebControls.TextBox txtMessage;

private void Page_Load(object sender, System.EventArgs e)
{
txtCountryCode.
Enabled = false;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent
();
base.OnInit(e);
}

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.Send.Click += new System.EventHandler(this.Send_Click);
this.rdoType.SelectedIndexChanged += new System.EventHandler(this.rdoType_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Send_Click(object sender, System.EventArgs e)
{
if (txtEmailId.Text.Trim() == "")
{
lblMessage.
Visible = true;
lblMessage.
Text = "Please Enter Email ID";
txtEmailId.
Focus();
return
;
}
else if (txtMobileNo.Text.Trim() == "")
{
lblMessage.
Visible = true;
lblMessage.
Text = "Please Enter Mobile No";
txtMobileNo.
Focus();
return
;
}
else if (txtMessage.Text.Trim() == "")
{
lblMessage.
Visible = true;
lblMessage.
Text = "Please Enter Message";
txtMessage.
Focus();
return
;
}
else
{
try
{
SmsTest.
net.webservicex.www.SendSMS smsIndia = new SmsTest.net.webservicex.www.SendSMS();
SmsTest.
com.webservicex.www.SendSMSWorld smsWorld = new SmsTest.com.webservicex.www.SendSMSWorld();
if (rdoType.SelectedValue == "1")
smsIndia.
SendSMSToIndia(txtMobileNo.Text.Trim(), txtEmailId.Text.Trim(), txtMessage.Text);
else
smsWorld.
sendSMS(txtEmailId.Text.Trim(), txtCountryCode.Text.Trim(), txtMobileNo.Text.Trim(), txtMessage.Text);
lblMessage.
Visible = true;
lblMessage.
Text = "Message Send Succesfully";
}
catch (Exception ex)
{
lblMessage.
Visible = true;
lblMessage.
Text = "Error in Sending message" + ex.ToString();
}
}
}

private void rdoType_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(rdoType.SelectedValue =="1")
txtCountryCode.
Enabled = false;
else
txtCountryCode.
Enabled = false;

}

protected void Clear_Click(object sender, EventArgs e)
{
txtEmailId.
Text = "";
txtCountryCode.
Text = "";
txtMobileNo.
Text = "";
txtMessage.
Text = "";
lblMessage.
Visible = false;
txtEmailId.
Focus();
}
}
}


And here is the .aspx page code.

<%@ Page Language="c#" CodeBehind="SendSms.aspx.cs" AutoEventWireup="false" Inherits="SmsTest.WebForm1" %>

<html>
<head>
<title>Sending Text Messages with .NETtitle>
head>
<body>
<table width="100%" border="1px">
<tr>
<td align="center">
<h2>
Send SMS
h2>
td>
tr>
<tr>
<td align="center">
<asp:Label ID="lblMessage" runat="server" ForeColor="#009900" Visible="False">asp:Label>
td>
tr>
table>
<form method="post" runat="server" id="Form1">
<table width="100%" border="1px">
<tr>
<td colspan="2" height="22">
&nbsp;
<asp:RadioButtonList ID="rdoType" runat="server" AutoPostBack="True" RepeatDirection="Horizontal"
Width
="304px">
<asp:ListItem Value="1" Selected="True">SMS to Indiaasp:ListItem>
<asp:ListItem Value="2">SMS to worldasp:ListItem>
asp:RadioButtonList>
td>
tr>
<tr>
<td>
Email Id
td>
<td>
<asp:TextBox ID="txtEmailId" runat="server">asp:TextBox>( ex :&nbsp;test@test.com
)
td>
tr>
<tr>
<td>
Country Code
td>
<td>
<asp:TextBox ID="txtCountryCode" runat="server" Text="">asp:TextBox>&nbsp;(ex
: 91 for&nbsp;India
td>
tr>
<tr>
<td>
Mobile
/cell Number
td>
<td>
<asp:TextBox ID="txtMobileNo" runat="server" Text="">asp:TextBox>&nbsp;( ex :&nbsp;984xxxxxxxx&nbsp;)
td>
tr>
<tr>
<td>
Message
td>
<td>
<asp:TextBox ID="txtMessage" runat="server" Rows="4" TextMode="MultiLine">asp:TextBox>
td>
tr>
<tr>
<td>
td>
<td>
<asp:Button ID="Send" runat="server" Text="Send">asp:Button>
<asp:Button ID="Clear" runat="server" Text="Clear" OnClick="Clear_Click">asp:Button>
td>
tr>
table>
form>
body>
html>


I have taken the idea from one of the leading sites. You can refer the main article here. Source.

I hope now you can easily send SMS using this application.

happy coding!!!!!!!!

Thanks
Anil Kumar Pandey
System Architect
Green Point Technology (India) Ltd.
Mumbai, Maharshtra

Kontera