How to Send SMS under Windows Using Connected to the PC Phone with Datacable/Bluetooth/IrDA/PCCard/etc. in C#

петък, 24 юли 2009 г.


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?

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;

private static extern IntPtr FindWindowEx(IntPtr hwndParent,
IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

private static extern int SendMessage(int hWnd, uint Msg,
int wParam, int lParam);


#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)
Console.WriteLine("SMS sent successfully!");

hwndMessageBox = FindWindowEx(IntPtr.Zero, IntPtr.Zero,
"#32770", "SMS Sender Error");
if (hwndMessageBox != IntPtr.Zero)
Console.WriteLine("SMS sending failed!");


private static void SendSMS(string phoneNumber, string smsBody)
Process process = new Process();
process.StartInfo.FileName =
Path.GetDirectoryName(Application.ExecutablePath) +
process.StartInfo.Arguments =
string.Format("/p:{0} /m:\"{1}\"", phoneNumber, smsBody);
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

Thread thread = new Thread(new ThreadStart(CloseMessageBox));


static void Main(string[] args)
switch (args.Length)
case 2:
SendSMS(args[0], args[1]);


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.

Vesko Kolev

11 коментара:

Stanislav каза...

Nice article, I should try.
Any Ideas how to run this on my Mac?

Мирослав Джоканов каза...

It's a really beneficial article. I admire your choice to share experience, Vesko.
However, your source invoked in me thoughts that it would be really useful to have some kind of "Win32 course" at FMI :D

Veselin Kolev каза...

Thanks, Miroslav.

I am happy to hear from you as you are one of these guys who "doesn't sleep" and makes everything to be on the top!

Hope will hear from you soon again.

Vesko Kolev

Sudhamshu каза...

Thank you for sharing the article. The hint for adding DeviceName in Registry editor was very helpful. Are there similar (simple) tools for reading SMS from phones you know of?

Himansu Mallick каза...

When i am running this code i am getting an error as "Access is denied" at process.start().

I am running this code in windows7.

Please give me the solution


Shashibhushan Gokhale каза...

Are you aware of some way (application, software, Microsoft Windows tool to call as SMSSender is to SMS) to dial phone numbers under Windows Using Connected to the PC Phone with Datacable/Bluetooth/IrDA/PCCard/etc.

AnAr Marketing каза...

Thank you for sharing the article. .NET Software Development India

nettyfish каза...

Fantastic articles is post by you in this blog
<a href="

Bulk sms provider chennai каза...

Thanks for sharing the article nice one

Rosy Jack каза...

You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...
Digital Mobile Marketing
SMS Marketing

Bulk sms provider chennai каза...

Thanks for sharing the Informative article would love to read your all the article

thanks again

Vesko Kolev's Blog : IDeveloper -