Main Screen Of Bulk SMS Sender
Introduction
A simple SMS application using free GSM Communication Library GSMComm for sending bulk SMS to multiple numbers of recipients using an Excel file for our all messages and recipients numbers
Background
Very helpfull for learning ...How to send SMS through C# using free GSM Communication Library with minimal efforts :-)
Using the code
So now lets go on the actual code , First step you need to get connect your GSM Phone / GSM Modem to your PC , Assuming that you have connected your device to PC and all the nessacery drivers are loaded and it is Connected Successfully.
For connecting device using GSM COMMUNICATION LIBRARY you have to download GSM COM LIb from http://www.scampers.org and reference it in your project , Then using it in your project
Expand|Select|Wrap|Line Numbers
- using GsmComm.GsmCommunication;
- using GsmComm.Interfaces;
- using GsmComm.PduConverter;
- using GsmComm.Server;
Expand|Select|Wrap|Line Numbers
- public static Int16 Comm_Port = 0;
- public static Int32 Comm_BaudRate = 0;
- public static Int32 Comm_TimeOut = 0;
- public static GsmCommMain comm;
All the COM Port List in the System will appear in the Grid Box as shown above
For Getting the COM Port List and additional data of COM Ports , Mircosoft has provided us an Utility Called "WMI CODE CREATOR" Which can easily create C# code for that purpose Google It and download to your PC it will give you thousands Classes / Methods / Properties to get the system information with minimal efforts of system programming.
Here how i get all the COM Port Information with all the addtional device information
//
// Here we are getting the all available information from the Win32_SerialPort and setting it in to the DataGrid
Expand|Select|Wrap|Line Numbers
- try
- {
- ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_SerialPort");
- foreach (ManagementObject queryObj in searcher.Get())
- {
- if (queryObj != null)
- {
- object captionObj = queryObj["DESCRIPTION"];
- object capdeviceid = queryObj["DEVICEID"];
- object MaxBaudRate = queryObj["MAXBAUDRATE"];
- object connstatus = queryObj["STATUS"];
- string timeoutsec = "100";
- dataGridView3.Rows.Add(capdeviceid, captionObj, MaxBaudRate, timeoutsec, connstatus);
- }}}
If without error you get all the COM Port Information from system then Second step is to connect to the device , Click on the Cell of Data Grid of your desired device to connect , As shown below....A Confirmation message will appear and Connected device information will also be shown and DataGrid Cell will Highlighted Green.
How to Connect to GSM Device / Phone , Try GSMComm connection with the values getting from the DataGrid when Cell Clicked
Expand|Select|Wrap|Line Numbers
- //FOR GsmCommMain we give three arguments (PORT , BAUD RATE , TIMEOUT SEC)
- Comm_Port = Convert.ToInt16(dataGridView3.Rows[i].Cells[0].Value.ToString().Substring(3));
- Comm_BaudRate = Convert.ToInt32(dataGridView3.Rows[i].Cells[2].Value.ToString());
- Comm_TimeOut = Convert.ToInt32(dataGridView3.Rows[i].Cells[3].Value.ToString());
- comm = new GsmCommMain(Comm_Port, Comm_BaudRate, Comm_TimeOut);
- try
- {
- comm.Open();
- if (comm.IsConnected())
- {
- //Something to do when device connected
- }
- Getting Phone Information after successfull connection
- //Getting phone information through IdentificationInfo Structures of GSMComm ...
- try
- {
- Phone_Name.Text = comm.IdentifyDevice().Manufacturer.ToUpper().ToString();
- Phone_Model.Text = comm.IdentifyDevice().Model.ToUpper().ToString();
- Revision_Num.Text = comm.IdentifyDevice().Revision.ToUpper().ToString();
- Serial_Num.Text = comm.IdentifyDevice().SerialNumber.ToUpper().ToString();
- }
Lets Check Out Single SMS Sending TAB
Expand|Select|Wrap|Line Numbers
- //For sending single SMS with minimal code
- //For SmsSubmitPdu we give three arguments (SMS TEXT , RECIPIENTS NUMBER , ENCODING )
- string CELL_Number, SMS_Message;
- SmsSubmitPdu pdu1;
- CELL_Number = Cell_Num.Text.ToString();
- SMS_Message = SMS_Text.Text.ToString();
- try
- {
- pdu1 = new SmsSubmitPdu(SMS_Message, CELL_Number, "");
- comm.SendMessage(pdu1);
- MessageBox.Show("M E S S A G E - S E N T", "Information", MessageBoxButtons.OK,
- MessageBoxIcon.Information);
- }
//Below code on button click will Open File Select Dialog Box for Excel File which contains Sheet name SMS and only
two columns named CELL NUMBER and MESSAGES which will be loaded in to DataGridView ,
Also you need to rename your Excel sheet to SMS because it also checking that sheet name
Expand|Select|Wrap|Line Numbers
- private void button3_Click_1(object sender, EventArgs e)
- {
- int rows_counting, column_counting1 = 0;
- OpenFileDialog dialog = new OpenFileDialog { };
- dialog.Filter = "SMS Sending File(*.xlsx;*.xls)|*.xlsx;*.xls";
- dialog.Title = "Select Excel File For SMS";
- DialogResult dlgresult = dialog.ShowDialog();
- if (dlgresult == DialogResult.Cancel)
- {
- MessageBox.Show("You Cancelled !!!");
- }
- else
- {
- string sms_filename = dialog.FileName;
- if (System.IO.File.Exists(sms_filename))
- {
- try
- {
- Cursor.Current = Cursors.WaitCursor;
- string connectionString = String.Format(
- @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};
- Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""",
- sms_filename);
- string query = String.Format("select * from [{0}$]", "SMS");
- OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
- dataSet = new DataSet();
- dataAdapter.Fill(dataSet);
- dataGridView1.DataSource = dataSet.Tables[0];
- dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
- rows_counting = dataGridView1.RowCount - 1;
- column_counting1 = dataGridView1.ColumnCount;
- if (column_counting1 < 2 || column_counting1 > 2)
- {
- MessageBox.Show("Kindly Check Column Count in Excel Sheet !!!\r\n\n
- There Should Be Only Two Columns in Sheet Like Below\r\n\n
- CELL NUMBER | MESSAGE", "Error", MessageBoxButtons.OK,
- MessageBoxIcon.Error);
- return;
- }
- if ( dataGridView1.Columns[0].Name.ToString().ToUpper() == "CELL NUMBER" &&
- dataGridView1.Columns[1].Name.ToString().ToUpper() == "MESSAGES")
- {
- label25.Text = "Total SMS In Excel File " + rows_counting;
- MessageBox.Show("Data Imported Successfully...!!!\r\n\n
- Check Imported Values & SEND SMS.....!!!!",
- "Information", MessageBoxButtons.OK,
- MessageBoxIcon.Information);
- }
- else
- {
- MessageBox.Show("Column Names Are Not In Specified Format !!!",
- "Error", MessageBoxButtons.OK,
- MessageBoxIcon.Error);
- return;
- }
- }
- catch (Exception E6)
- {
- MessageBox.Show("Error Loading Excel FIle\r\n\nKindly Check Worksheet Name",
- "Error", MessageBoxButtons.OK,
- MessageBoxIcon.Error);
- return;
- }
- }}}
Expand|Select|Wrap|Line Numbers
- string MSMS_Number, MMessage;
- int i;
- SmsSubmitPdu pdu3;
- try
- {
- if (comm.IsConnected()==true)
- {
- try
- {
- for (i = 0; i < dataGridView1.RowCount - 1; i++)
- {
- MSMS_Number = dataGridView1.Rows[i].Cells[0].Value.ToString();
- MMessage = dataGridView1.Rows[i].Cells[1].Value.ToString();
- pdu3 = new SmsSubmitPdu(MMessage, MSMS_Number, "");
- comm.SendMessage(pdu3);
- //Sleeps system for 1000ms for refreshing GSM Modem / Phone
- System.Threading.Thread.Sleep(1000);
- }
- Cursor.Current = Cursors.Default;
- MessageBox.Show("T O T A L - M E S S A G E - S E N T = " + i,
- "Information", MessageBoxButtons.OK,
- MessageBoxIcon.Information);
- }
And for extra spice i have added simple checking when Excel File is loaded in to DataGridView when CHECK VALUES button is clicked it will check for NULL VALUES and Message text more than 160 Characters and highlighted cells to RED if found error else cells will be green if found no error....
For acheving cell checking a simple yet once again FOR LOOP will do the Magic ;-)
//On Check Values button below code will check cell values One By One and set the color either RED or GREEN
if IF STATEMENT passes
Expand|Select|Wrap|Line Numbers
- private void button7_Click(object sender, EventArgs e)
- {
- for (int i = 0; i < dataGridView1.RowCount - 1; i++)
- {
- for (int j = 0; j < dataGridView1.ColumnCount ; j++)
- {
- if ( dataGridView1.Rows[i].Cells[j].Value.ToString() == "" ||
- dataGridView1.Rows[i].Cells[j].Value.ToString().ToUpper() == "-" ||
- dataGridView1.Rows[i].Cells[j].Value.ToString().Length > 160 )
- //Setting Cellls Background color to RED if above Error Found in Any of the cells
- dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
- //Setting Cells Background color to GREEN which passes above validations
- else
- dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Green;
- button4.Enabled = true;
- }}}
Points of Interest
You Should Try WMI CODE CREATOR It Will Give Lots Of Help To Explore System Information
History
This is Actually a much smaller version of code made for a much larger project for some specific purpose for sending Bulk SMS for fellow Team Members :-)
Would be glad if you like to have any help...
Bulk SMS SENDER Ver 1.0