Ozeki.net.sms

If you are curious how to send and receive SMS messages through your Ozeki NG SMS gateway with a C# application, you just need to read this guide carefully. Since, it has all the necessary information. It shows you step-by-step what you need to do, in order to make your Ozeki net.sms work.

Download example from nuget.org: http://www.nuget.org/packages/ozeki.net.sms/

Prerequisites

Install nuget package manager

With Installing the NuGet Package Manager, you can download and easily update the project (SMSClient and the corresponding sample program). The NuGet is available for Visual Studio 2010, 2012.

Install to Visual Studio 2010:

An extension can be installed into the Professional, Premium, and Ultimate editions. First of all select the ’Tools’ menu and ’Extension Manager…’. In Extension Manager select Online Gallery, and click on the ’Search Online Gallery’ field in the upper right corner. Then type in the NuGet word. After finding the ‘NuGet Package Manager’ extension, click to the ’Download’ button next to it, and follow the installation steps (Figure 1).

download nuget package manager
Figure 1 - Download the NuGet Package Manager

Install to Visual Studio 2012:

The NuGet is included in every edition (except Team Foundation Server) by default. Updates of NuGet can be found through the Extension Manager. You can check whether your copy of Visual Studio already has the NuGet extension, look for Library Package Manager in the Tools menu of Visual Studio. If your copy of Visual Studio does not have the Library Package Manager (NuGet) extension yet, you can install it by using the Extension Manager.

Download project

After you have installed the NuGet Package Manager, you can download the project by Package Manager Console. Select the ’Tools’ menu -> ’Library Package Manager’ -> ’Package Manager Console’ (Figure 2).

opening of package manager console
Figure 2 - Opening of the Package Manager Console

In the Package Manager Console execute the following command:

Install-Package ozeki.net.sms

Code Example 1 - Installing the project from Package Manager Console

In the Solution Explorer, you can see the added new SMSClient Reference, and the two added class file (SmsHanderSample.cs, TestSms.cs).

Write the example

You have downloaded, and installed the project. For manage SMS sending and receiving, you can open the SmsHandlerSample.cs file. In the constructor of the class, you will see the following lines:

public SmsHandlerSample()
{
	//Create SMS client
	mySMSClient = new ozSMSClient();

	//Wire up SMS client events
	mySMSClient.OnClientConnected += mySMSClient_OnClientConnected;
	mySMSClient.OnClientDisconnected += mySMSClient_OnClientDisconnected;
	mySMSClient.OnClientConnectionError += mySMSClient_OnClientConnectionError;
	mySMSClient.OnMessageAcceptedForDelivery += mySMSClient_OnMessageAcceptedForDelivery;
	mySMSClient.OnMessageDeliveredToNetwork += mySMSClient_OnMessageDeliveredToNetwork;
	mySMSClient.OnMessageDeliveredToHandset += mySMSClient_OnMessageDeliveredToHandset;
	mySMSClient.OnMessageDeliveryError += mySMSClient_OnMessageDeliveryError;
	mySMSClient.OnMessageReceived += mySMSClient_OnMessageReceived;
}

Code Example 2 - Constructor of the SmsHandlerSample

In the constructor you will recognise, the initialisation of ozSMSClient, this client will forward the messages between the sample project and the Ozeki NG SMS Gateway. After the initialisation you can see the subscribing for the events of ozSMSClient. All of these events will write to the console the corresponding events messages. The next interesting method is the Connect():

public bool Connect(string host, int port, string username, string password)
    {
        mySMSClient.Username = username;
        mySMSClient.Password = password;
        mySMSClient.Host = host;
        mySMSClient.Port = port;

        mySMSClient.Connected = true;
        if (!mySMSClient.Connected)
            Console.WriteLine(DateTime.Now + " " + "Could not connect to SMS Gateway. Reason: " + mySMSClient.getLastErrorMessage());

        return mySMSClient.Connected;
    }

You can call this method through the SmsHandlerSample class. The parameters are referring to the Ozeki NG SMS Gateway details. The host is the address of the server, and the port is squarely the number of the port what is used. The username and the password have to be valid, and existing. After configure all of the settings, the mySMSClient trying to login with the given data (by set and validate the connected state).
Now, try to call the SendSMS method, through an SmsHandlerSample class, that method looks like this:

public void SendSMS(string recipihent, string messageContent)
{
	string msgID = mySMSClient.sendMessage(recipient, messageContent);
} 

Code Example 3 - Method for sending SMS message

After the message sent, the NG will trigger the onMessageDeliveredToNetwork. This event is invoked by the SMS Gateway when the SMS message was accepted by the mobile network for delivery. Another event is triggered when the recipient device got the message (onMessageDeliveredToHandset). The third event what can be fired is the OnMessageReceived event. This event is triggered when the configured user (username, password) is got a message. Then it will forward the message to the subscriber objects, so you will get the message in your subscribing method:

private void mySMSClient_OnMessageReceived(object sender, DeliveryEventArgs e)
{
	Console.WriteLine(DateTime.Now + " " + "Message received. Sender address: " + e.Senderaddress + " Message text: " + e.Messagedata + "rn");
}

Code Example 4 - Handle incoming messages with that method

Of course you can use the full Ozeki SMSClient as you wish. The SMS sending is just a simple sample from that.

Test the example

You have done with the Ozeki NG SMS Gateway settings. Open your Visual Studio, and install the project if you have not done that before. Open the TestSms.cs source file. Change the parameters of the methods to yours:

	_smsHandlerSample = new SmsHandlerSample();
	if (!_smsHandlerSample.Connect("localhost", 9500, "admin", "12345"))
		return;
	_smsHandlerSample.SendSMS("123456789", "TestMessage");

Code Example 5 - Instantiate of SmsHandlerSample, and sending SMS

You can read some information about parameters in the constructor of the SmsHandlerSample class. After you have finished with actualising of parameters, you can run the project. Press F5 to do that. If your settings were correct, you will receive an SMS to your device by the following line:

	_smsHandlerSample.SendSMS("123456789", "TestMessage");

Code Example 6 - Sending SMS messages

You can test the incoming messages by sending and SMS to the configured user.

If you have any questions or need assistance, please contact us at  info@ozekiphone.com

Dig deeper!
People who read this also read...

More information