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.
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.
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
)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]
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 recive the sms.
Create the new Desktop application.
Design the form as shown in the picture.
Dear Reader, please read this carefully in thais step we need to include the following DLL files in the bin\ Debug folder:
Now we need to add the reference to the project as shown in the picture.
Now we have added one more reference.
Now we need to move to the code behind window and use these references in the code.
Now we need to add few textboxes and private variables that we will use in our code.
Now we will add the two classes Operation and Main as in the following and add two methods in it GetDBConnection, ipconfig and Execute.
Now add the following code in the form load event.
Now add the following code in the connect button click event.
Now add the following code in the send button click event.
Note: Dear reader please doesn'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.
- SQL Server instance na
- SQL Server user name and password.(user id sa password.###Reno123)
- 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");
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 2 (let this application to be installed and close it when it is finished).
Now run the application; press F5.
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.
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.
Now press the "Send SMS button"; you will then get a SMS on your mobile phone as in the following:
NOTE: Dear reader, the code works 100%; please contact me if you have any query regrarding this article.
Download File: GSMModem.zip
source code of the GSM Modem.