петък, 24 юли 2009 г.
Intro
Today I had to solve a very interesting task for one of our projects at NASD – sending SMSes trough .NET and C#. In the past I have used different kinds of services for sending SMSes – SOAP web services, Email2SMS services, etc. The usage of the above methods is very straightforward, doesn’t require any special knowledge and the task is practically a trivial. Our application required sending of very small number of SMSes, so we have thought of another approach – to connect in some way (Datacable/Bluetooth/IrDA/ PCCard/etc.) a physical device to the PC and to send messages trough it.
In this post I will show you ready to use approach to send SMSes from a connected to a Windows PC GSM device that supports SMS sending.
The Possibilities
In general there are two possible approaches for solving the task. The first one is by consuming some API or already written library trough C#/Java/etc. I started with the idea of using Java and J2ME for developing the application. However as a C# and .NET fan, I decided firstly to research if there is something .NETish which I could use. A lot of comercial COM libraries could be found on the market, which therefore can be consumed from almost every Windows based language/development platform.
The other main possibility was to find some command line enabled tool, which could be wrapped by a C#/Java/etc.
After a short research my conclusion was that non comercial library which works could hardly be found. So I started looking for the other possibility – a command line tool. But what happened?
Solution
I was very happy to find something called SMSSender provided by Microsoft. SMSSender is an add-on for Microsoft Windows XP that will create and send SMS (short text messages) by using your GSM cellular phone. It also allows the use of all characters from international alphabets. SMSSender could be used under Windows Server 2003 and Windows Vista (with setting to run it in compatibility mode for Windows XP), too. The application has a very simple and userfriendly UI and can be used easily as a standalone application.
To send an SMS firstly you should select your already connected device from which you want to send the SMS. Then you need to input the destination number and of course the content of the message itself. When ready, you can click Send. If everything is OK, you will see the following message box:
On the other hand – if something fails you will see message box, simmilar to this one:
After a minute you will find why I am talking about this message boxes at all.
The Command Arguments
As I said above, the SMSSender application is command argument enabled application. The arguments which we are interested in are the /p:phone_number and /m:”sms_body”. Here we should ask ourselves what happens with the third option – the device selection. Unfortunately the tool doesn’t have argument for device selection. If we don’t provide such the application argues that no device was selected. But how we could select this option? There are two ways possible was to do the default device selection – the first one is to set the registry key HKEY_CURRENT_USER\Software\Microsoft\SMSSender and add new string value called DeviceName with data field equals to the name of your device (in my current case LGE Mobile USB Modem or in other words the value from the dropdown above). The alternative way is just to enable logging (going to Options and check the Enable Logging checkbox).
The SendSMS Wrapper
OK we are ready to implement the wrapper. The task seems very straightforward – something like 10 to 15 minutes – start the process, hide its window, wait the process to end, etc. But is there something which could be a problem for us and make the writing of such application more challenging? If you remember, when clicking the Send button (which is equivalent to start the application with the appropriate command arguments) there was a message box which waits one to click OK(no matter successful or not). Yes.. I know such a behavior seems very developer unfriendly with this pup up messages. When looking for a setting/argument to disable these alerts, I wasn’t able to find such. So I had to put my past Win32 experience in practice again and had to close this message box with code.
I’ll explain this part of the code in short – after starting the process of the SMSSender, I start a thread, which polls for a window with a specific caption (using the native Win32 function FindWindowEx) with a given delay. After finding either the successful or the failed message box, it sends closing message to this window (again with a native Win32 function, this time SendMessage) and prints to the console the whether the message was sent or not. After this the application ends.
The Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
namespace SendSMS
{
class Program
{
#region Win32 API Import
public const int WM_COMMAND = 0x0112;
public const int WM_CLOSE = 0xF060;
[DllImport("user32.dll")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent,
IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, uint Msg,
int wParam, int lParam);
#endregion
#region Methods
private static void ShowHelp()
{
Console.WriteLine("Vesko Kolev's SendSMS usage:");
Console.WriteLine("SendSMS phone_number \"sms_body\"");
Console.WriteLine("SendSMS 0812345678 \"Test sms body!\"");
}
private static void CloseMessageBox()
{
IntPtr hwndMessageBox;
while (true)
{
hwndMessageBox = FindWindowEx(IntPtr.Zero,
IntPtr.Zero, "#32770", "SMS Sender");
if (hwndMessageBox != IntPtr.Zero)
{
SendMessage(hwndMessageBox.ToInt32(),
WM_COMMAND, WM_CLOSE, 0);
Console.WriteLine("SMS sent successfully!");
break;
}
hwndMessageBox = FindWindowEx(IntPtr.Zero, IntPtr.Zero,
"#32770", "SMS Sender Error");
if (hwndMessageBox != IntPtr.Zero)
{
SendMessage(hwndMessageBox.ToInt32(),
WM_COMMAND, WM_CLOSE, 0);
Console.WriteLine("SMS sending failed!");
break;
}
Thread.Sleep(500);
}
}
private static void SendSMS(string phoneNumber, string smsBody)
{
Process process = new Process();
process.StartInfo.FileName =
Path.GetDirectoryName(Application.ExecutablePath) +
@"\MSSMSSender.exe";
process.StartInfo.Arguments =
string.Format("/p:{0} /m:\"{1}\"", phoneNumber, smsBody);
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
Thread thread = new Thread(new ThreadStart(CloseMessageBox));
thread.Start();
process.WaitForExit();
}
static void Main(string[] args)
{
switch (args.Length)
{
case 2:
SendSMS(args[0], args[1]);
break;
default:
ShowHelp();
break;
}
}
#endregion
}
}
Conclusion
In this article I have showed you a ready to use source code which sends SMSes from a connected to your PC GSM device. You can consume this app virtually from anywhere – just wrap the wrapper and forget about this task!
I’ll be happy to hear your comments about everything related.
Thanks,
Vesko Kolev