Click here to Skip to main content
11,635,098 members (77,892 online)
Click here to Skip to main content

Bulk SMS Sender

, 18 Aug 2013 CPOL 140.6K 38.9K 112
Rate this:
Please Sign up or sign in to vote.
Sending multiple / bulk SMS through Excel file to multiple recipients using GSM phone or GSM modem

Main Start-up Screen

Main Screen Of Bulk SMS Sender

Introduction

This is a simple SMS application using free GSM Communication Library GSMComm for sending bulk SMS to multiple numbers of recipients using an Excel file for all our messages and recipients' numbers.

Background

It is very helpful for learning how to send SMS through C# using free GSM Communication Library with minimal efforts.

Using the Code

So now let's go on the actual code. The first step is that you need to get connected your GSM Phone / GSM Modem to your PC assuming that you have connected your device to PC and all the necessary 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 use it in your project:

using GsmComm.GsmCommunication;
using GsmComm.Interfaces;
using GsmComm.PduConverter;
using GsmComm.Server;

And construct the initial necessary information for connection and GSMComm class.

public static Int16 Comm_Port = 0;
public static Int32 Comm_BaudRate = 0;
public static Int32 Comm_TimeOut = 0;
public static GsmCommMain comm;

Bingo! After connecting your device to your PC, run the project and the above shown main screen will appear, now press Get COM Port List button on main screen to get all Ports information in to the DataGrid.

COM Port List Of System

All the COM Port Lists in the System will appear in the Grid Box as shown above.

For getting the COM Port List and additional data of COM Ports, Microsoft has provided us a 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 of classes / methods / properties to get the system information with minimal efforts of system programming.

Here is how I get all the COM Port Information with all the additional device information:

//
//     Here we are getting the all available information from the Win32_SerialPort 
//     and setting it in to the DataGrid

        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);
                    }}}

var i = C#; 

If you get all the COM Port Information from system without error, then the 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 be highlighted green.

Connection Confirmation

Connection Confirmation

How to Connect to GSM Device / Phone

Try GSMComm connection with the values got from the DataGrid when Cell is Clicked.

//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 successful 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();
}

After successfully connecting with the device, now it's time to send a word out there. Smile | :)

Let's Check Out Single SMS Sending TAB

For Sending Single SMS Single SMS Message Sent

//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);
}

For sending multiple SMS from Excel file, here's how you can achieve 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 is also checking that sheet name

        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 | MESSAGES", "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;
                    }
            }}}

After loading Excel file into the DataGrid, we'll only use the For Loop function to get CELL NUMBER & MESSAGE one by one and send it using very simple code just like for sending a single SMS message.

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);
                }

Simple, isn't it? If not, send me a question and I will be glad to help you out.

And for extra spice, I have added simple checking when Excel File is loaded into 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 an error is found, or else cells will be green if no error is found.

For achieving cell checking, a simple yet once again FOR LOOP will do the magic. Wink | ;-)

//On Check Values button below code will check cell values 
//One By One and set the color either RED or GREEN
//if <b>IF STATEMENT</b> passes

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 Cells 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;
         }}}

That's all folks... happy coding... Let the SMS begin !!! Smile | :)

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 to fellow Team Members. Smile | :)

Bulk SMS SENDER Ver 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Fahad Rafiq
Pakistan Pakistan
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionhilp me. Pin
hazrat sh14-Apr-13 21:38
memberhazrat sh14-Apr-13 21:38 
AnswerRe: hilp me. Pin
Fahad Rafiq1-May-13 7:32
memberFahad Rafiq1-May-13 7:32 
QuestionConverting from Excel to Text Format Pin
Member 997792610-Apr-13 1:52
memberMember 997792610-Apr-13 1:52 
AnswerRe: Converting from Excel to Text Format Pin
Fahad Rafiq1-May-13 7:29
memberFahad Rafiq1-May-13 7:29 
QuestionUnable to work out how to send using excel Pin
Member 99738239-Apr-13 9:02
memberMember 99738239-Apr-13 9:02 
AnswerRe: Unable to work out how to send using excel Pin
Fahad Rafiq1-May-13 7:24
memberFahad Rafiq1-May-13 7:24 
QuestionNokia C1-01problem Pin
Member 91959987-Apr-13 17:47
memberMember 91959987-Apr-13 17:47 
AnswerRe: Nokia C1-01problem Pin
Fahad Rafiq10-Apr-13 2:47
memberFahad Rafiq10-Apr-13 2:47 
QuestionHow can i send USSD Code ? Pin
MKHasan5-Mar-13 11:06
memberMKHasan5-Mar-13 11:06 
AnswerRe: How can i send USSD Code ? Pin
Fahad Rafiq21-Mar-13 0:26
memberFahad Rafiq21-Mar-13 0:26 
GeneralMy vote of 5 Pin
MKHasan5-Mar-13 10:40
memberMKHasan5-Mar-13 10:40 
GeneralRe: My vote of 5 Pin
Fahad Rafiq10-Mar-13 19:19
memberFahad Rafiq10-Mar-13 19:19 
GeneralMy vote of 5 Pin
Member 982544214-Feb-13 21:24
memberMember 982544214-Feb-13 21:24 
GeneralRe: My vote of 5 Pin
Fahad Rafiq19-Feb-13 5:29
memberFahad Rafiq19-Feb-13 5:29 
Hi Member 9825442 Smile | :)

Thanks for your vote mate Thumbs Up | :thumbsup:


BR
Fahad

GeneralRe: My vote of 5 Pin
Member 997986514-Apr-13 22:14
memberMember 997986514-Apr-13 22:14 
GeneralRe: My vote of 5 Pin
Fahad Rafiq17-Apr-13 18:48
memberFahad Rafiq17-Apr-13 18:48 
QuestionGSM phone with USB data cabel Pin
Mohammad Umer10-Feb-13 18:47
memberMohammad Umer10-Feb-13 18:47 
AnswerRe: GSM phone with USB data cabel Pin
Fahad Rafiq19-Feb-13 5:48
memberFahad Rafiq19-Feb-13 5:48 
GeneralRe: GSM phone with USB data cabel Pin
Mohammad Umer19-Feb-13 19:38
memberMohammad Umer19-Feb-13 19:38 
GeneralRe: GSM phone with USB data cabel Pin
Fahad Rafiq19-Feb-13 20:25
memberFahad Rafiq19-Feb-13 20:25 
GeneralRe: GSM phone with USB data cabel Pin
Mohammad Umer19-Feb-13 20:55
memberMohammad Umer19-Feb-13 20:55 
GeneralRe: GSM phone with USB data cabel Pin
Fahad Rafiq20-Feb-13 1:17
memberFahad Rafiq20-Feb-13 1:17 
GeneralRe: GSM phone with USB data cabel Pin
Mohammad Umer20-Feb-13 1:24
memberMohammad Umer20-Feb-13 1:24 
GeneralRe: GSM phone with USB data cabel Pin
Fahad Rafiq21-Feb-13 0:11
memberFahad Rafiq21-Feb-13 0:11 
Generalhow to receive sms? Pin
ars8829-Jan-13 8:47
memberars8829-Jan-13 8:47 
GeneralRe: how to receive sms? Pin
Fahad Rafiq19-Feb-13 5:23
memberFahad Rafiq19-Feb-13 5:23 
GeneralMy vote of 3 Pin
maxsshukla23-Jan-13 7:04
membermaxsshukla23-Jan-13 7:04 
GeneralRe: My vote of 3 Pin
Fahad Rafiq25-Jan-13 23:51
memberFahad Rafiq25-Jan-13 23:51 
QuestionError retrieving COM port phone information Pin
Serendipty A5-Dec-12 20:26
memberSerendipty A5-Dec-12 20:26 
QuestionError on uploading excel file Pin
AsifAbbasZaidi19-Nov-12 5:53
memberAsifAbbasZaidi19-Nov-12 5:53 
AnswerRe: Error on uploading excel file Pin
Fahad Rafiq20-Nov-12 3:37
memberFahad Rafiq20-Nov-12 3:37 
GeneralRe: Error on uploading excel file Pin
AsifAbbasZaidi23-Nov-12 19:58
memberAsifAbbasZaidi23-Nov-12 19:58 
GeneralRe: Error on uploading excel file Pin
AsifAbbasZaidi23-Nov-12 20:12
memberAsifAbbasZaidi23-Nov-12 20:12 
GeneralMy vote of 5 Pin
Akash Kumar Singh16-Nov-12 3:52
memberAkash Kumar Singh16-Nov-12 3:52 
GeneralRe: My vote of 5 Pin
Fahad Rafiq16-Nov-12 20:20
memberFahad Rafiq16-Nov-12 20:20 
QuestionExcel upload not working Pin
Member 959787013-Nov-12 19:52
memberMember 959787013-Nov-12 19:52 
AnswerRe: Excel upload not working Pin
Fahad Rafiq16-Nov-12 20:28
memberFahad Rafiq16-Nov-12 20:28 
GeneralRe: Excel upload not working Pin
Serendipty A21-Nov-12 17:32
memberSerendipty A21-Nov-12 17:32 
SuggestionGr8 Job Bro, Pin
shani Jee1-Nov-12 4:11
membershani Jee1-Nov-12 4:11 
GeneralRe: Gr8 Job Bro, Pin
Fahad Rafiq16-Nov-12 20:15
memberFahad Rafiq16-Nov-12 20:15 
QuestionNot working Pin
Raghavendra Rao 55828-Oct-12 21:30
memberRaghavendra Rao 55828-Oct-12 21:30 
AnswerRe: Not working Pin
Fahad Rafiq16-Nov-12 20:12
memberFahad Rafiq16-Nov-12 20:12 
Questionconnection terminated Pin
sushil145928-Aug-12 22:11
membersushil145928-Aug-12 22:11 
AnswerRe: connection terminated Pin
Fahad Rafiq17-Oct-12 2:20
memberFahad Rafiq17-Oct-12 2:20 
GeneralRe: connection terminated Pin
MarkoSc5-Feb-13 1:35
memberMarkoSc5-Feb-13 1:35 
QuestionBulk SMS Pin
fujale26-Aug-12 3:05
memberfujale26-Aug-12 3:05 
AnswerRe: Bulk SMS Pin
Fahad Rafiq17-Oct-12 2:17
memberFahad Rafiq17-Oct-12 2:17 
Suggestionposting the code Pin
rashidmv24-Jul-12 0:30
memberrashidmv24-Jul-12 0:30 
Questionincluding additional features in sms sending app Pin
rashidmv18-Jul-12 19:57
memberrashidmv18-Jul-12 19:57 
Generalgratitude Pin
rashidmv18-Jul-12 19:44
memberrashidmv18-Jul-12 19:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150728.1 | Last Updated 19 Aug 2013
Article Copyright 2012 by Fahad Rafiq
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid