Now you can send sms directly from your desktop using C#. Just connect your GSM modem to your computer and use the following code to build Your own sms software.
Now to build your own SMS software using visual studio in C# Just follow the following steps carefully.
Adding Namespaces:
using System.IO;
using System.IO.Ports;
///////////-Your Form Load Event-///////////////////////////
Now to build your own SMS software using visual studio in C# Just follow the following steps carefully.
Adding Namespaces:
using System.IO;
using System.IO.Ports;
///////////-Your Form Load Event-///////////////////////////
try
{
#region Display all available COM Ports
string[] ports = SerialPort.GetPortNames();
// Add all port names to the combo box:
foreach (string port in ports)
{
this.cboPortName.Items.Add(port);
}
#endregion
//Remove tab pages
this.tabSMSapplication.TabPages.Remove(tbSendSMS);
this.tabSMSapplication.TabPages.Remove(tbReadSMS);
this.tabSMSapplication.TabPages.Remove(tbDeleteSMS);
this.btnDisconnect.Enabled = false;
}
catch(Exception ex)
{
ErrorLog(ex.Message);
}
//////////////-Connecting to GSM Modem-///////
try
{
//Open communication port
this.port = objclsSMS.OpenPort(this.cboPortName.Text, Convert.ToInt32(this.cboBaudRate.Text), Convert.ToInt32(this.cboDataBits.Text), Convert.ToInt32(this.txtReadTimeOut.Text), Convert.ToInt32(this.txtWriteTimeOut.Text));
if (this.port != null)
{
//this.tabSMSapplication.TabPages.Remove(tbPortSettings);
this.gboPortSettings.Enabled = false;
MessageBox.Show("Modem is connected at PORT " + this.cboPortName.Text,"Free Csharp Tutorials.blogspot.Com");
//Add tab pages
this.tabSMSapplication.TabPages.Add(tbSendSMS);
this.tabSMSapplication.TabPages.Add(tbReadSMS);
this.tabSMSapplication.TabPages.Add(tbDeleteSMS);
this.lblConnectionStatus.Text = "Connected at " + this.cboPortName.Text;
this.btnDisconnect.Enabled = true;
}
else
{
MessageBox.Show("Invalid port settings");
}
}
catch (Exception ex)
{
ErrorLog(ex.Message);
}
/////////-Disconnecting GSM Modem-////////////////////////
try
{
this.gboPortSettings.Enabled = true;
objclsSMS.ClosePort(this.port);
//Remove tab pages
this.tabSMSapplication.TabPages.Remove(tbSendSMS);
this.tabSMSapplication.TabPages.Remove(tbReadSMS);
this.tabSMSapplication.TabPages.Remove(tbDeleteSMS);
this.lblConnectionStatus.Text = "Not Connected";
this.btnDisconnect.Enabled = false;
}
catch (Exception ex)
{
ErrorLog(ex.Message);
}
//////-Sending MSG -//////////////////////////////////////
try
{
if (objclsSMS.sendMsg(this.port, this.txtSIM.Text, this.txtMessage.Text))
{
MessageBox.Show("Message has sent successfully");
}
else
{
MessageBox.Show("Failed to send message");
}
}
catch (Exception ex)
{
ErrorLog(ex.Message);
}
}
private void btnReadSMS_Click(object sender, EventArgs e)
{
try
{
//count SMS
int uCountSMS = objclsSMS.CountSMSmessages(this.port);
if (uCountSMS > 0)
{
// If SMS exist then read SMS
#region Read SMS
objShortMessageCollection = objclsSMS.ReadSMS(this.port);
foreach (ShortMessage msg in objShortMessageCollection)
{
ListViewItem item = new ListViewItem(new string[] { msg.Index ,msg.Sender, msg.Message });
item.Tag = msg;
lvwMessages.Items.Add(item);
}
#endregion
}
else
{
lvwMessages.Clear();
MessageBox.Show("There is no message in SIM");
}
}
catch (Exception ex)
{
ErrorLog(ex.Message);
}
}
//////////////////////// Reading SMS ///////////////////////////////
try
{
//count SMS
int uCountSMS = objclsSMS.CountSMSmessages(this.port);
if (uCountSMS > 0)
{
// If SMS exist then read SMS
#region Read SMS
objShortMessageCollection = objclsSMS.ReadSMS(this.port);
foreach (ShortMessage msg in objShortMessageCollection)
{
ListViewItem item = new ListViewItem(new string[] { msg.Index ,msg.Sender, msg.Message });
item.Tag = msg;
lvwMessages.Items.Add(item);
}
#endregion
}
else
{
lvwMessages.Clear();
MessageBox.Show("There is no message in SIM");
}
///////////////////////////////////-Deleting SMS From Modem SIM-/////////////////////////
try
{
//Count SMS
int uCountSMS = objclsSMS.CountSMSmessages(this.port);
if (uCountSMS > 0)
{
DialogResult dr = MessageBox.Show("Are u sure u want to delete the SMS?", "Delete confirmation", MessageBoxButtons.YesNo);
if (dr.ToString() == "Yes")
{
#region Delete SMS
if (this.rbDeleteAllSMS.Checked)
{
#region Delete all SMS
string strCommand = "AT+CMGD=1,4";
if (objclsSMS.DeleteMsg(this.port, strCommand))
{
MessageBox.Show("Messages has deleted successfuly ");
}
else
{
MessageBox.Show("Failed to delete messages ");
}
#endregion
}
else if (this.rbDeleteReadSMS.Checked)
{
//...............................................Delete Read SMS ....................................................
#region Delete Read SMS
string strCommand = "AT+CMGD=1,3";
if (objclsSMS.DeleteMsg(this.port, strCommand))
{
MessageBox.Show("Messages has deleted successfuly ");
}
else
{
MessageBox.Show("Failed to delete messages ");
}
#endregion
}
#endregion
}
}
}
catch (Exception ex)
{
ErrorLog(ex.Message);
}
}
private void btnCountSMS_Click(object sender, EventArgs e)
{
try
{
//Count SMS
int uCountSMS = objclsSMS.CountSMSmessages(this.port);
this.txtCountSMS.Text = uCountSMS.ToString();
}
catch (Exception ex)
{
ErrorLog(ex.Message);
}
}
//////////////////////////////////////-Error Log -////////////////////////
public void ErrorLog(string Message)
{
StreamWriter sw = null;
try
{
string sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";
string sPathName = @"E:\";
string sYear = DateTime.Now.Year.ToString();
string sMonth = DateTime.Now.Month.ToString();
string sDay = DateTime.Now.Day.ToString();
string sErrorTime = sDay + "-" + sMonth + "-" + sYear;
sw = new StreamWriter(sPathName + "SMSapplication_ErrorLog_" + sErrorTime + ".txt", true);
sw.WriteLine(sLogFormat + Message);
sw.Flush();
}
catch (Exception ex)
{
ErrorLog(ex.ToString());
}
finally
{
if (sw != null)
{
sw.Dispose();
sw.Close();
}
}
}
#endregion
}
}

No comments:
Post a Comment