Send SMS Using GSM Modem With C#

Dear reader, today we will discuss a very important topic, sending SMS alerts to a mobile phone that is a common demand of most clients. I have experience developing donation collection software. In that software we used a GSM Modem to send a SMS thanks to the donor. So we developed a small application that runs on the server and sends a SMS whenever a new record is inserted into the database from any client.

In this application we will use the following:

  • GSM Modem (I have used Vodafone; you can use any)
  • SIM Card.
    GSM1.jpg

Step 1

Create the table in SQL Server.

CREATE TABLE [dbo].[Donor_Profile](
      [Don_Date] [datetime] NULL,
      [Don_Donor_ID] [varchar](20) NOT NULL,
      [Don_Donor_Name] [varchar](50) NULL,
      [Don_Mobile_Phone] [varchar](50) NULL,
      [Don_Member] [varchar](10) NULL,
      [Don_SMS1] [varchar](10) NULL,
      [Don_SMS2] [varchar](10) NULL,
      [Don_Logo] [image] NULL,
      [Don_Donor_Type] [int] NULL,
 CONSTRAINT [PK_Donor_Profile] PRIMARY KEY CLUSTERED
(
      [Don_Donor_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

Step 2

Now you can insert any record in the table but please make sure to insert:

[Don_Donor_ID]= CF-00001-12 and leave the [Don_SMS1], [Don_SMS2] field NULL, [Don_Mobile_Phone] please enter valid phone number where you want to receive the SMS.

Step 3

Create the new Desktop application.

GSM2.jpg

Step 4

Design the form as shown in the picture.

GSM3.jpg

Step 5

Dear Reader, please read this carefully in this step we need to include the following DLL files in the bin\ Debug folder:

  • GSMCommServer.dll
  • GSMCommShared.dll
  • GSMCommunication.dll
  • PDUConverter.dll

Step 6

Now we need to add the reference to the project as shown in the picture.

  GSM4.jpgGSM5.jpg

Step 7

Now we have added one more reference.

GSM6.jpg

Step 8

Now we need to move to the code behind window and use these references in the code.

GSM7.jpg

Step 9

Now we need to add few textboxes and private variables that we will use in our code.

GSM8.jpg

Step 10

Now we will add the two classes Operation and Main as in the following and add two methods in it GetDBConnection, ipconfig and Execute.

GSM9.jpg

GSM10.jpg

Step 11

Now add the following code in the form load event.

GSM11.jpg


Step 12

Now add the following code in the connect button click event.

GSM12.jpg

Step 13

Now add the following code in the send button click event.

GSM13.jpg

Note: Dear reader please don't worry, I have attached the source file for your reference. No need to type in the entire code. But you need to change the SQL Server setting correspondingly for your own system.
For example:

  1. SQL Server instance na

    me.(.\\CRMIS)
     
  2. SQL Server user name and password.(user id sa password.###Reno123)
  3. Initial Catalog=PSH (database name)

Add the public static class in the Main.cs as in the following:

public static SqlConnection GetDBConnection()
{
  SqlConnection conn = new SqlConnection(
 "Data Source=.\\CRMIS;Initial Catalog=PSH;User ID=sa;Password=###Ren0321");
 return conn;
}

Step 14

Now we need to attach the modem to the system. It will install its own application but keep this point in your mind, you need to close the application after the installation is complete because that software captures the port that we will use in our code.

Picture 1

GSM14.jpg

Picture 2 (let this application to be installed and close it when it is finished).

GSM15.jpg

Step 15

Now run the application; press F5.

GSM16.jpg

First we need to select the port name from the drop-down menu then press the connect button; if port is not valid then it will show the error message.

GSM17.jpg

Now I will select port COM4; it will connect successfully. Dear reader, you need to check your port one by one; Windows XP normally uses port COM3 or COM4.

GSM18.jpg

Step 16

Now press the "Send SMS button"; you will then get a SMS on your mobile phone as in the following:

GSM19.jpg

NOTE: Dear reader, the code works 100%; please contact me if you have any query regrarding this article.


Recommended Free Ebook
Similar Articles