How to Send SMS in C# Using GSM Modem/Dongle


How to Send SMS in C# Using GSM Modem/Dongle
This is a very simple method to send SMS via GSM Modem so without much explanations let's get into it.

Thing's you'll need :
  • GSM Modem with a SIM
  • Libraries :
    GSMCommServer.dll
    GSMCommShared.dll
    GSMCommunication.dll
    PDUConverter.dll
If you've installed the Modem software you'll be able find all the libraries needed. Just go to add Reference and add the mentioned libraries (without them this program won't work).

How to Send SMS in C# Using GSM Modem/Dongle

Now go to your form and add control as given below
  • 4 Labels :
    Label1 - Message Body :
    Label2 - Phone Number :
    Label3 - Port :
    Label4 - Modem Not Connected
  • 2 Textboxes :
    Textbox1 - To Phone Number
    Textbox2 - To Message Body
  • 1 ComboBox :
    Combobox1 - To Port :
  • 2 Buttons
    Button1 - Send
    Button2 - Connect
 Arrange your form like this :

How to Send SMS in C# Using GSM Modem/Dongle

 Now let's go to the coding

Namespaces :
 using GsmComm.GsmCommunication;  
 using GsmComm.PduConverter;  
 using GsmComm.Server;  

Add this above Public Form1 :

 private GsmCommMain comm;  

Form1_Load :
 Combobox1.Items.Add("COM1");  
 Combobox1.Items.Add("COM2");  
 Combobox1.Items.Add("COM3");  
 Combobox1.Items.Add("COM4");  
 Combobox1.Items.Add("COM5");  

Button2_Click :
 if (Combobox1.Text == "")  
       {  
         MessageBox.Show("Invalid Port Name");  
         return;  
       }  
       comm = new GsmCommMain(Combobox1.Text, 9600, 150);  
       Cursor.Current = Cursors.Default;  
       bool retry;  
       do  
       {  
         retry = false;  
         try  
         {  
           Cursor.Current = Cursors.WaitCursor;  
           comm.Open();  
           MessageBox.Show("Modem Connected Sucessfully");  
           Button2.Enabled = false;  
           label4.Text = "Modem is connected";  
         }  
         catch (Exception)  
         {  
           Cursor.Current = Cursors.Default;  
           if (MessageBox.Show(this, "GSM Modem is not available", "Check",  
             MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) == DialogResult.Retry)  
             retry = true;  
           else  
           { return; }  
         }  
       }  
       while (retry);  

Button1_Click :
 try  
       {  
         Cursor.Current = Cursors.WaitCursor;  
         SmsSubmitPdu pdu;  
           Cursor.Current = Cursors.Default;  
           byte dcs = (byte)DataCodingScheme.GeneralCoding.Alpha7BitDefault;  
           pdu = new SmsSubmitPdu(Textbox2.Text, Textbox1.Text);  
           int times = 1;  
           for (int i = 0; i < times; i++)  
           {  
             comm.SendMessage(pdu);  
           }  
         MessageBox.Show("Message Sent Succesfully","Success",MessageBoxButtons.OK,MessageBoxIcon.Information);  
       }  
       catch(Exception ex)  
       {  
         MessageBox.Show(ex.Message.ToString());  
       }  

That's all, Do share your comments.


Credits : Rotich