Monday, December 30, 2013

How can i send and receive sms from windows application without using any third party API?

Hi Faisal Z,


The C# implementation of the code is as follows:



using System;
using System.Threading;
using System.ComponentModel;
using System.IO.Ports;

public class SMSCOMMS
{
private SerialPort SMSPort;
private Thread SMSThread;
private Thread ReadThread;
public static bool _Continue = false;
public static bool _ContSMS = false;
private bool _Wait = false;
public static bool _ReadPort = false;
public delegate void SendingEventHandler(bool Done);
public event SendingEventHandler Sending;
public delegate void DataReceivedEventHandler(string Message);
public event DataReceivedEventHandler DataReceived;

public SMSCOMMS(ref string COMMPORT)
{
SMSPort = new SerialPort();
SMSPort.PortName = COMMPORT;
SMSPort.BaudRate = 9600;
SMSPort.Parity = Parity.None;
SMSPort.DataBits = 8;
SMSPort.StopBits = StopBits.One;
SMSPort.Handshake = Handshake.RequestToSend;
SMSPort.DtrEnable = true;
SMSPort.RtsEnable = true;
SMSPort.NewLine = System.Environment.NewLine;
ReadThread = new Thread(
new System.Threading.ThreadStart(ReadPort));
}

public bool SendSMS(string CellNumber, string SMSMessage)
{
string MyMessage = null;
//Check if Message Length <= 160
if (SMSMessage.Length <= 160)
MyMessage = SMSMessage;
else
MyMessage = SMSMessage.Substring(0, 160);
if (IsOpen == true)
{
SMSPort.WriteLine("AT+CMGS=" + CellNumber + "r");
_ContSMS = false;
SMSPort.WriteLine(
MyMessage + System.Environment.NewLine + (char)(26));
_Continue = false;
if (Sending != null)
Sending(false);
}
return false;
}

private void ReadPort()
{
string SerialIn = null;
byte[] RXBuffer = new byte[SMSPort.ReadBufferSize + 1];
string SMSMessage = null;
int Strpos = 0;
string TmpStr = null;
while (SMSPort.IsOpen == true)
{
if ((SMSPort.BytesToRead != 0) & (SMSPort.IsOpen == true))
{
while (SMSPort.BytesToRead != 0)
{
SMSPort.Read(RXBuffer, 0, SMSPort.ReadBufferSize);
SerialIn =
SerialIn + System.Text.Encoding.ASCII.GetString(
RXBuffer);
if (SerialIn.Contains(">") == true)
{
_ContSMS = true;
}
if (SerialIn.Contains("+CMGS:") == true)
{
_Continue = true;
if (Sending != null)
Sending(true);
_Wait = false;
SerialIn = string.Empty;
RXBuffer = new byte[SMSPort.ReadBufferSize + 1];
}
}
if (DataReceived != null)
DataReceived(SerialIn);
SerialIn = string.Empty;
RXBuffer = new byte[SMSPort.ReadBufferSize + 1];
}
}
}

public bool SendSMS(string CellNumber, string SMSMessage)
{
string MyMessage = null;
if (SMSMessage.Length <= 160)
{
MyMessage = SMSMessage;
}
else
{
MyMessage = SMSMessage.Substring(0, 160);
}
if (IsOpen == true)
{
SMSPort.WriteLine("AT+CMGS=" + CellNumber + "r");
_ContSMS = false;
SMSPort.WriteLine(
MyMessage + System.Environment.NewLine + (char)(26));
_Continue = false;
if (Sending != null)
Sending(false);
}
return false;
}

public void Open()
{
if (IsOpen == false)
{
SMSPort.Open();
ReadThread.Start();
}
}

public void Close()
{
if (IsOpen == true)
{
SMSPort.Close();
}
}

}

Then use the code as below:



SMSEngine = new SMSCOMMS("COM1");
SMSEngine.Open();
SMSEngine.SendSMS("919888888888","THIS IS YOUR MESSAGE");
SMSEngine.Close();

For more information about this sample code, see:http://www.codeproject.com/Articles/19023/Sending-SMS-using-NET





We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

Click HERE to participate the survey.


No comments:

Post a Comment