This article is about sending SMS using C#.NET through MS SQL Server.
It is intended for web developers with a basic knowledge of the
C#.NET and SQL technologies. The reader should be familiar with Microsoft
Visual Studio and the C# programming language. The page contains a
downloadable source code (see above), a list of aims you can achieve using
the code, a list of prerequisites,
a description and depiction of the architecture and operation of the system,
step-by-step instructions about how to use the code and a detailed interpretation.
It also includes a brief summary, answers to frequently asked questions, as well
as links to related information.
When to use this code
This code is particularly useful for those who wish to
add SMS functionality to an application.
create automated SMS notifications.
increase application security by adding SMS login.
Prerequisites
The following table lists the software requirements of the
system.
Note that you can download Ozeki NG - SMS Gateway and .NET Framework 4.8
on the download page.
Operating system:
Windows 2019, Windows 2016, Windows 2012 R2, Windows 2008, Windows 2008 R2, Windows 7, Windows 8 or Windows 10
Other software requirements:
.NET Framework 4.0
Ozeki NG - SMS Gateway Server Microsoft SQL Server 2019 (Express Edition) or newer Microsoft SQL Server
Development platform:
Microsoft Visual Studio .NET
How it works
To send SMS messages from C#.NET applications,
you need to install Ozeki NG - SMS
Gateway and Microsoft SQL Server on your computer. Ozeki NG - SMS Gateway
will use a
GSM phone/modem attached to your PC
(with a phone-to-PC data cable) or an
IP SMS connection
to send messages. Your C#.NET application will insert a row into the database
to send messages using the Ozeki NG program. For a better understanding of how
it works, please look at the following diagram (Figure 1).
Figure 1 - Sending SMS from an C#.NET application
In the diagram you can see a user
with the C#.NET application, an SQL Server, Ozeki NG - SMS Gateway, a mobile
phone attached to the server computer and a mobile user receiving the message.
Wherever the user is, if their C# application is appropriately configured
(the configuration requires information related to the MS SQL server), they
can compose and send messages to any recipients.
After the user's action, the C#.NET application inserts
a row with the data of the SMS to be sent, which will be selected by
Ozeki NG - SMS Gateway from the database. Ozeki NG will forward the message
to the GSM network through a mobile phone attached to the PC with a data cable,
and the mobile user will receive the SMS message.
Configure the MS SQL Server
Step 1: To configure Microsoft SQL Server 2019,
first install
SQL Server Management Studio Express.
Step 2: Next, start SQL Server Management Studio Express and log in.
Step 3: If you would like to use a username and password to log in,
you have to
set the authentication mode. Picture help Step 4: Now create the user and the database with tables.
Follow this picture help.
Create a Database user in Ozeki NG - SMS Gateway
Step 1: Open Ozeki NG's graphical user interface (Start menu ->
All Programs ->
OzekiNG -> Manage OzekiNG). Picture help Step 2: Create the database user in Ozeki NG with the necessary data
following
the instructions.
Using the code
To use the downloaded C# code, follow these steps:
Step 1:
Unpack the downloaded zip file.
Step 2:
Set/Change the fixed data in the DatabaseHandling.cs file (e.g., server
name, user name, password).
Picture help
Step 4:
Start the Ozeki NG - SMS Gateway Server (if not running).
Picture help
Step 5:
Build and run it by MS Visual Studio .NET
Step 6:
Click the Compose message button, fill in the necessary fields,
and click the "Send" button.
Picture help
Understanding the code
The downloadable code inserts the message to be sent into the ozekimessageout
database table. Ozeki NG - SMS Gateway periodically checks the table, and if
it finds a row where the status is send, it will try to send it. In the
script you have to set the server name to where the MS SQL Server is running,
the username (specifying who can log in to the MS SQL Server) with the
password and the type of the SMS message.
The source code of the example application is structured in the following way:
MainForm.cs:
Shows and handles a "Compose message" button and 2 ListView items for
incoming and outgoing messages.
ComposeMessageForm.cs:
Shows and handles the necessary data fields for creating a new message.
DatabaseHandling.cs:
Handles the database and refreshes the 2 ListView items.
Description of the process depicted in Figure 1 above:
Step 1:Creating the form
Through MainForm.cs in the ComposeMessageForm.cs file, you create the
form that requests the sms data.
The User fills in the necessaries. The user will be asked to fill in the
Recipient and Message text fields.
A pop-up messagebox is used to inform the User about error(s) or result(s)
of sending the message.
In MainForm.cs file you set the 2 ListView items (view mode and names of
columns). In
the buttonComposeMessage_Click(...) procedure the ComposeMessageForm is called
to compose a new message.
After closing that box the 2 ListView items will be refreshed.
After filling in the fields and clicking the Send button, the application
receives the information about the form.
In the ComposeMessageForm.cs file, the CheckAndSendMessage() procedure is
called by "buttonSend_Click(...)".
At the beginning of the procedure, check the data of the textbox fields. The
Recipient box is mandatory. If it is empty, the processing will be aborted,
and the User will be informed about the error. If the checking is
successful, you will insert a new row into the ozekimessageout database table
(in DatabaseHandling.insertMessage(...)).
We need the following: the address of the computer running the Microsoft
SQL Server [the default address is (local)\SQLExpress
((local) means that MS SQL Server is installed on the same
computer on which the C#.NET application is running)], the username (who is
authorized
to log in to the MS SQL Server and to insert messages, the user's password,
the message type (the default is SMS:TEXT), the recipient and the message data.
ComposeMessageForm.cs
...
private void buttonSend_Click(object sender, EventArgs e)
{
CheckAndSendMessage();
}
private void CheckAndSendMessage()
{
if (textBoxRecipient.Text == "")
{
MessageBox.Show("Recipient field mustn't be empty!",
"Incorrect field value");
return;
}
string errorMsg = "";
DatabaseHandling.insertMessage(textBoxRecipient.Text,
textBoxMessageText.Text, out errorMsg);
MessageBox.Show(errorMsg, "Result of inserting message");
}
...
Step 3:Checking the incoming and
the outgoing message table
By refreshing the table of incoming and outgoing messages, you can check the
incoming messages and the status of
outgoing messages. Look at the updateListViewOut() procedure. First, you clear
its rows.
Then, you create the connection. If it is successful, you will refill the
outgoing messages table.
In the article it has been explained how you can add SMS functionality to your
website
by using the downloadable C#.NET example code. You can freely use and modify it.
Using a C#.NET application (and the Ozeki NG - SMS Gateway Server) you can
create a lot
of useful services (e.g.: you can add SMS functionality to an application,
create automated SMS notifications, increase application security
by adding SMS login, etc.).
Frequently asked questions
Question: How can I send different message
types?
Answer: For example, a Wap push message:
<si>
<indication href="http://target_address" action="signal-high">
text of description
</indication>
</si>
The action can be one of the following: signal-high, signal-medium,
signal-low, signal-none, signal-delete.
Question: Can I run the C# application on a
computer different
from the one running the Ozeki NG - SMS Gateway Server and the MS SQL
Server?
Answer: Yes, you can. In the application code you have to set the
Server in the "connectionString"
of the computer running the MS SQL Server.
Question: Can I set the phone number so that
the recipient can see where the sms comes from?
Answer: Yes, you can. In the code you have to set the sender field
too. It works
only if you have IP SMS connection.