public class SendSms
{
static AutoResetEvent readNow = new AutoResetEvent(false);
static SerialPort port;
private static string ExecuteCommand(string command, int timeout)
{
byte[] replyData = new byte[80];
port.DiscardInBuffer();
port.DiscardOutBuffer();
readNow.Reset();
port.Write(command + "\r");
port.Read(replyData, 0, replyData.Length);
return ASCIIEncoding.ASCII.GetString(replyData);
}
private static SerialPort EstablishConnection(string portName)
{
SerialPort port = new SerialPort();
if (portName.ToUpper().StartsWith("COM") == false) portName = "COM" + portName;
port.PortName = portName;
port.BaudRate = 9600;
port.DataBits = 8;
port.StopBits = StopBits.One;
port.Parity = Parity.None;
port.Encoding = Encoding.GetEncoding("iso-8859-1");
port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
port.Open();
port.DtrEnable = true;
port.RtsEnable = true;
return port;
}
private static void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (e.EventType == SerialData.Chars)
readNow.Set();
}
public static bool Send(string Mobile, string Message, string portname)
{
try
{
port = EstablishConnection(portname);
}
catch
{
return false;
}
string recievedData = ExecuteCommand("AT", 300);
recievedData = ExecuteCommand("AT+CMGF=1", 300);
String command = "AT+CMGS=\"" + Mobile + "\"";
recievedData = ExecuteCommand(command, 300);
string message = Message;
command = message + char.ConvertFromUtf32(26) + "\r";
recievedData = ExecuteCommand(command, 300);
if (recievedData.EndsWith("\r\nOK\r\n")) recievedData = "Message sent successfully";
if (recievedData.Contains("ERROR"))
{
port.Close();
return false;
}
else
{
port.Close();
return true;
}
}
}
Take this as class file. before launching this connect the mobile to the computer, and note the port number of the mobile connected to system and give that to portNumber vairiable.