473,421 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,421 developers and data experts.

Bulk SMS Sender in C#

1
Main Start-up Screen
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
  1. using GsmComm.GsmCommunication;
  2. using GsmComm.Interfaces;
  3. using GsmComm.PduConverter;
  4. using GsmComm.Server;
  5.  
And construct the initial nessecary info for connection and GSMComm classe.

Expand|Select|Wrap|Line Numbers
  1.         public static Int16 Comm_Port = 0;
  2.         public static Int32 Comm_BaudRate = 0;
  3.         public static Int32 Comm_TimeOut = 0;
  4.         public static GsmCommMain comm;
  5.  
Bingo after connecting your device to your PC run the project and above shown Main Screen will appears, Now press Get COM Port List button on main screen to get all Ports information in to the DataGrid



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
  1.         try
  2.             {
  3.                 ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_SerialPort");                                       
  4.                 foreach (ManagementObject queryObj in searcher.Get())
  5.                 {
  6.                     if (queryObj != null)
  7.                     {
  8.                         object captionObj = queryObj["DESCRIPTION"];
  9.                         object capdeviceid = queryObj["DEVICEID"];
  10.                         object MaxBaudRate = queryObj["MAXBAUDRATE"];
  11.                         object connstatus = queryObj["STATUS"];
  12.                         string timeoutsec = "100";
  13.  
  14.                             dataGridView3.Rows.Add(capdeviceid, captionObj, MaxBaudRate, timeoutsec, connstatus);
  15.                     }}}
  16.  
  17.  

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
  1.  
  2. //FOR GsmCommMain we give three arguments (PORT , BAUD RATE , TIMEOUT SEC)
  3.  
  4.                     Comm_Port =     Convert.ToInt16(dataGridView3.Rows[i].Cells[0].Value.ToString().Substring(3));
  5.                     Comm_BaudRate = Convert.ToInt32(dataGridView3.Rows[i].Cells[2].Value.ToString());
  6.                     Comm_TimeOut =  Convert.ToInt32(dataGridView3.Rows[i].Cells[3].Value.ToString());
  7.  
  8.                     comm = new GsmCommMain(Comm_Port, Comm_BaudRate, Comm_TimeOut);
  9.  
  10.                 try
  11.                 {
  12.                         comm.Open();
  13.                         if (comm.IsConnected())
  14.                         {
  15.                 //Something to do when device connected
  16.                         }
  17.  
  18. Getting Phone Information after successfull connection
  19.  
  20. //Getting phone information through IdentificationInfo Structures of GSMComm ...
  21.  
  22.                         try
  23.                         {
  24.                             Phone_Name.Text = comm.IdentifyDevice().Manufacturer.ToUpper().ToString();
  25.                             Phone_Model.Text = comm.IdentifyDevice().Model.ToUpper().ToString();
  26.                             Revision_Num.Text = comm.IdentifyDevice().Revision.ToUpper().ToString();
  27.                             Serial_Num.Text = comm.IdentifyDevice().SerialNumber.ToUpper().ToString();
  28.                         }
  29.  
After Successfully connecting with the Device ...Now it's time to send a word out there :-)

Lets Check Out Single SMS Sending TAB



Expand|Select|Wrap|Line Numbers
  1. //For sending single SMS with minimal code 
  2.  
  3. //For SmsSubmitPdu we give three arguments (SMS TEXT , RECIPIENTS NUMBER , ENCODING )
  4.  
  5.                 string CELL_Number, SMS_Message;
  6.  
  7.                 SmsSubmitPdu pdu1;
  8.  
  9.                     CELL_Number = Cell_Num.Text.ToString();
  10.                     SMS_Message = SMS_Text.Text.ToString();
  11.  
  12.                     try
  13.                     {
  14.                         pdu1 = new SmsSubmitPdu(SMS_Message, CELL_Number, "");
  15.                         comm.SendMessage(pdu1);
  16.                         MessageBox.Show("M E S S A G E - S E N T", "Information", MessageBoxButtons.OK, 
  17.                 MessageBoxIcon.Information);
  18.  
  19.                     }
  20.  
For sending multiple SMS from Excel file here's how you can acheive this .....



//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
  1.         private void button3_Click_1(object sender, EventArgs e)
  2.         {
  3.             int rows_counting, column_counting1 = 0;
  4.             OpenFileDialog dialog = new OpenFileDialog { };
  5.             dialog.Filter = "SMS Sending File(*.xlsx;*.xls)|*.xlsx;*.xls";
  6.             dialog.Title = "Select Excel File For SMS";
  7.             DialogResult dlgresult = dialog.ShowDialog();
  8.             if (dlgresult == DialogResult.Cancel)
  9.             {
  10.                 MessageBox.Show("You Cancelled !!!");
  11.             }
  12.             else
  13.             {
  14.                 string sms_filename = dialog.FileName;
  15.  
  16.                 if (System.IO.File.Exists(sms_filename))
  17.                 {
  18.                     try
  19.                     {
  20.                         Cursor.Current = Cursors.WaitCursor;
  21.  
  22.                         string connectionString = String.Format(
  23.                 @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};
  24.                 Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", 
  25.                 sms_filename);
  26.  
  27.                         string query = String.Format("select * from [{0}$]", "SMS");
  28.                         OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
  29.                         dataSet = new DataSet();
  30.                         dataAdapter.Fill(dataSet);
  31.                         dataGridView1.DataSource = dataSet.Tables[0];
  32.                         dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
  33.                         rows_counting = dataGridView1.RowCount - 1;                        
  34.                         column_counting1 = dataGridView1.ColumnCount;
  35.  
  36.                         if (column_counting1 < 2 || column_counting1 > 2)
  37.                         {
  38.                             MessageBox.Show("Kindly Check Column Count in Excel Sheet !!!\r\n\n
  39.                                There Should Be Only Two Columns in Sheet Like Below\r\n\n
  40.                                CELL NUMBER | MESSAGE", "Error", MessageBoxButtons.OK, 
  41.                                MessageBoxIcon.Error);                            
  42.                             return;
  43.                         }
  44.  
  45.                         if (    dataGridView1.Columns[0].Name.ToString().ToUpper() == "CELL NUMBER" &&
  46.                                 dataGridView1.Columns[1].Name.ToString().ToUpper() == "MESSAGES")
  47.  
  48.                         {
  49.                             label25.Text = "Total SMS In Excel File " + rows_counting;
  50.                             MessageBox.Show("Data Imported Successfully...!!!\r\n\n
  51.                                Check Imported Values & SEND SMS.....!!!!", 
  52.                               "Information", MessageBoxButtons.OK, 
  53.                                MessageBoxIcon.Information);
  54.                         }
  55.                         else
  56.                         {
  57.                             MessageBox.Show("Column Names Are Not In Specified Format !!!", 
  58.                               "Error", MessageBoxButtons.OK, 
  59.                                MessageBoxIcon.Error);
  60.                             return;
  61.                         }                       
  62.  
  63.                     }
  64.                     catch (Exception E6)
  65.                     {
  66.                         MessageBox.Show("Error Loading Excel FIle\r\n\nKindly Check Worksheet Name", 
  67.                             "Error", MessageBoxButtons.OK, 
  68.                              MessageBoxIcon.Error);
  69.                         return;
  70.                     }
  71.             }}}
  72.  
After loading excel file in to the DataGrid we'll only use For Loop function to get CELL NUMBER & MESSAGE One By One and send it using very simple code just like for sending Single SMS Message


Expand|Select|Wrap|Line Numbers
  1.         string MSMS_Number, MMessage;
  2.             int i;
  3.             SmsSubmitPdu pdu3;
  4.             try
  5.             {
  6.                 if (comm.IsConnected()==true)
  7.                     {
  8.                         try
  9.                         {
  10.                             for (i = 0; i < dataGridView1.RowCount - 1; i++)
  11.                             {
  12.  
  13.                                 MSMS_Number = dataGridView1.Rows[i].Cells[0].Value.ToString();
  14.                                 MMessage = dataGridView1.Rows[i].Cells[1].Value.ToString();
  15.                                 pdu3 = new SmsSubmitPdu(MMessage, MSMS_Number, "");
  16.                                 comm.SendMessage(pdu3);
  17.                                 //Sleeps system for 1000ms for refreshing GSM Modem / Phone
  18.                                 System.Threading.Thread.Sleep(1000);
  19.                             }
  20.  
  21.                         Cursor.Current = Cursors.Default;
  22.  
  23.                         MessageBox.Show("T O T A L - M E S S A G E - S E N T = " + i,
  24.                             "Information", MessageBoxButtons.OK, 
  25.                              MessageBoxIcon.Information);
  26.                         }
  27.  
Simple is'nt ....If not send me a question i will be glad to help you out :-)

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
  1.        private void button7_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.             for (int i = 0; i < dataGridView1.RowCount - 1; i++)
  5.             {
  6.                 for (int j = 0; j < dataGridView1.ColumnCount ; j++)
  7.                 {
  8.                     if (    dataGridView1.Rows[i].Cells[j].Value.ToString() == "" ||                        
  9.                             dataGridView1.Rows[i].Cells[j].Value.ToString().ToUpper() == "-" ||
  10.                             dataGridView1.Rows[i].Cells[j].Value.ToString().Length > 160 )
  11.  
  12.                         //Setting Cellls Background color to RED if above Error Found in Any of the cells
  13.  
  14.                         dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red;
  15.  
  16.                         //Setting Cells Background color to GREEN which passes above validations
  17.  
  18.                     else
  19.  
  20.                         dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Green;
  21.                         button4.Enabled = true;
  22.                 }}}
  23.  
Thats All Folks ......Happy Coding.........Let The SMS Begin !!! :-)

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
Attached Files
File Type: zip SMS Sender Program.zip (313.7 KB, 2652 views)
File Type: xlsx SMS.xlsx (8.4 KB, 1502 views)
File Type: zip SMS_SENDER_SOURCE_CODE.zip (659.1 KB, 3545 views)
Feb 8 '12 #1
0 29363

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: jason | last post by:
What are the technical challenges in getting a local SMTP email server set up on a win3k system or alternatively on a win2k pro local work statation. We are on the verge of acquiring a new win3k...
7
by: iqbal | last post by:
Hi all, We have an application through which we are bulk inserting rows into a view. The definition of the view is such that it selects columns from a table on a remote server. I have added the...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
6
by: Sean | last post by:
HI There, I am making the transition from asp to asp .net, I am currenty writing an application that requires a bulk insert from a webform into SQL server, normally I would just create rows of...
1
by: SteveH | last post by:
Attempting to use BULK INSERT to transfer data from a csv to a table in a dbo on an instance of sqlServer (MSDE). I’m very new at both sql server & vb.net. I keep getting an error Unexpected...
1
by: teddymeu | last post by:
Hi Guys, trying to bulk insert a csv file into my SQL database from an asp.net vb web app/form page that the user uploads, my problem is that im new to all this and although the SQL statement...
0
by: teddymeu | last post by:
Hi Guys, since I've done nothing but ask questions these last few weeks to get my first application up and running I thought it was about time to share the wealth and help out a newbie like me since...
2
by: AB | last post by:
Hi to all, I have a problem about a importation of a file *.csv with SQL Server, through a bulk insert, called in a store procedure that a c# sw calls. This is the description of the error:...
3
by: AB | last post by:
Hi to all, I have a problem about a importation of a file *.csv with SQL Server, through a bulk insert, called in a store procedure that a c# sw calls. This is the description of the error:...
1
by: dougancil | last post by:
I have the following code: Imports System.IO Imports System.Data Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.