Thursday, July 23, 2009

How to send Sms?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using MailtoSMS;

namespace MailtoSMS
{
class sendSMS
{
String PORT;
public sendSMS()
{
try
{
char[] ch = new char[10];
string reply;
string[] s = System.IO.Ports.SerialPort.GetPortNames();
for (int i = 0; i < s.Length; i++)
{
SerialPort sp1 = new SerialPort(s[i].ToString());
sp1.Open();
sp1.Write("at\r\n");
System.Threading.Thread.Sleep(3500);
reply = sp1.ReadExisting();
if (reply.Contains("OK"))
{
PORT = s[i].ToString();
sp1.Close();
break;
}
}
SerialPort sp = new SerialPort(PORT);
sp.Open();
sp.Write("at\r\n");
System.Threading.Thread.Sleep(3500);
reply = sp.ReadExisting();
if (reply.Contains("OK"))
{
try
{
int i = 26;
char ctrlz = (char)i;
//textBox1.Text = textBox1.Text + s.ToString() + "\r\n";
sp.Write("at+cmgf=1\r\n");
System.Threading.Thread.Sleep(3500);
reply = sp.ReadExisting();
sp.Write("at+cmgs=\"" + Form1.MobNo + "\"\r\n");
System.Threading.Thread.Sleep(3500);
sp.Write(Form1.message + ctrlz);
reply = sp.ReadExisting();
sp.Close();
sp.Dispose();
}
catch (ObjectDisposedException ex)
{
Console.WriteLine(ex.Message);
}
}

//for(int i=0; i
//textBox1.Text = textBox1.Text + s.ToString() + "\r\n";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

No comments:

Post a Comment