Wednesday, May 30, 2012

Sending & Receiving SMS from ASP.NET

Sending & Receiving SMS from ASP.NET application is nowadays integrate parts of enterprise web application specially in the domains of Healthcare, Hospitality, Insurance , Banking sectors where 24X7  available approach with customers are mandatory.
  • In those respect , I have used a code in C# ASP.NET 4.0 using XML API (Purchased from Value-First SMS Provider) to send SMS from our web application to our clients randomly.
  • So, first develop the GUI for sending SMS. So the following code in .aspx:
<%@Page Language=”C#”AutoEventWireup=”true”CodeBehind=”SMS.aspx.cs”Inherits=”SMSTest.SMS” %>

<!DOCTYPE htmlPUBLIC“-//W3C//DTD XHTML 1.0 Transitional//EN”“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title></title>
</head>
<body style=”height: 313px”>
<form id=”form1″runat=”server”>
<div>
<h1> Sending SMS Application </h1>
</div>
<p>
<asp:LabelID=”lblTo”runat=”server”Text=”To:-”></asp:Label>
</p>
<asp:TextBoxID=”txtTo”runat=”server”></asp:TextBox>
<p>
<asp:LabelID=”lblFrom”runat=”server”Text=”From:-”></asp:Label>
</p>
<asp:TextBoxID=”txtFrom”runat=”server”Width=”128px”></asp:TextBox>
<p>
<asp:LabelID=”lblBody”runat=”server”Text=”SMS Message:-”></asp:Label>
</p>
<p>
<asp:TextBoxID=”txtBody”  TextMode=”MultiLine”runat=”server”></asp:TextBox>
</p>
<asp:ButtonID=”btnSend”runat=”server”onclick=”btnSend_Click”Text=”Send”/>
<p>
<asp:Label ID=”lblResult”runat=”server”Text=” “></asp:Label>
</p>
</form>
</body>
</html>
  • Now , check in Code-behind.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Text;

namespace SMSTest
{
public partial class SMS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
System.Net.WebClient webClient = new System.Net.WebClient();
string postData = “usr=yourapiusername&pwd=yourapipwd&from= “ + txtFrom.Text + “&to=” + txtTo.Text +“&type=text” + txtBody.Text;
webClient.Headers.Add(“Content-Type”,“application/x-www-form-urlencoded”);
lblResult.Text = webClient.UploadString(“http://api.myvaluefirst.com/psms/servlet/psms.Eservice2?data=<?xml%20version=\”1.0\”%20encoding=\”ISO-8859-1\”?><!DOCTYPE%20MESSAGE%20SYSTEM%20\”http://127.0.0.1/psms/dtd/message.dtd\”%20><MESSAGE><USER%20USERNAME=\”Your SMS API Username” + “\”%20PASSWORD=\”Your SMS API Password” + “\”/><SMS%20UDH=\”0\”%20CODING=\”1\”%20TEXT=\”"+ txtBody.Text + “\”%20PROPERTY=\”0\”%20ID=\”1\”><ADDRESS%20FROM=\”919903013543\”%20TO=\”" + txtTo.Text +“\”%20SEQ=\”1\”%20TAG=\”some%20clientside%20random%20data\”%20/></SMS></MESSAGE>&action=send”, postData);
}
}
}

How to send SMS to Mobile using web application ASP.Net & C#?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.Mail;

using System.Web.Services.Protocols;

using System.Web.Services;



public partial class _Default : System.Web.UI.Page 

{

 protected void Page_Load(object sender, EventArgs e)

 {



 }

 protected void Button1_Click(object sender, EventArgs e)

 {

  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(TextBox3.Text.Trim(), 

     TextBox1.Text.Trim(), TextBox4.Text);

  else 

   smsWorld.sendSMS(TextBox1.Text.Trim(), 

     TextBox2.Text.Trim(), 

     TextBox3.Text.Trim(), TextBox4.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")

   TextBox2.Enabled = false;

 else

   TextBox2.Enabled = false;

}



}

How to send mail from web application using asp.net and c#?

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("2128675309@cingularme.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");