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

петък, 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

27 коментара:

Unknown каза...

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.

Regards,
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?

Unknown каза...

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

Thanks....

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.

Unknown каза...

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

sukere каза...

Fantastic articles is post by you in this blog
<a href="http://www.nettyfish.com

Bulk sms provider chennai каза...

Thanks for sharing the article nice one

Bulk sms provider chennai каза...

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

thanks again

Lantechsoft каза...

Very nice informative article about software and related information, its very nice article. thanks for sharing such great article hope keep sharing such kind of article advance android bulk sms sender app

Warnerhill каза...

It was very helpful to me. keep posting amazing stuff. Thanks Android Bulk SMS sender App

Smith каза...

Its very nice informative article. thanks for sharing such great article hope keep sharing such kind of article Bulk sms Sender software

Warnerhill каза...

Now a days sms marketing is trending for sales and Productivity.Thanks for sharing such great information. hope you keep sharing such kind of information Bulk sms software

Lantechsoft каза...

I Find it very informative about sms marketing,Thanks for sharing such great information. hope you keep sharing such kind of information how to send bulk message

Warnerhill каза...

Best information about software.Thanks for sharing such great information. hope you keep sharing such kind of information Android Bulk sms sender

Warnerhill каза...

Good content and a nice blog. Thanks for sharing

such great information. hope you keep sharing such

kind of information Android Bulk SMS Sender

Smith каза...

Now a day sms marketing is the best way to increase sale of you product.Thanks for sharing such great information. hope you keep sharing such kind of information sms sender software

Анонимен каза...

Blog is knowledgeable and such a lovely content you had share with us. worth it.
bulk sms service.

Lantechsoft каза...

Nice Article, SMS marketing does not necessitate tremendous planning or plotting. You need not draft a design, mock-up content or scale outlines for the ad Best sms marketing software

Warnerhill каза...

Nice Article, SMS marketing does not necessitate tremendous planning or plotting. You need not draft a design, mock-up content or scale outlines for the ad bulk sms sender

Cheapest Bulk SMS Provider каза...

Thanks A Lot For Sharing The Informative Blog On Bulk SMS ! Really Appreciated .
I Personally Really Appreciate Your Effort Man ! Keep It Up ! Looking For More Updates !
Have A Look At Our Site Also Bulk SMS Provider | Bulk SMS Service Provider . Really Loved This Blog .
Best Blog Ever On Blogspot.com !

Smith каза...

Nice articel. the best way to outreach to potential customers is to directly connect Bulk sms sender

Smith каза...

Nice article. the best way to outreach to potential customers is to directly connect Best sms marketing software

BULK SMS PLANS каза...

Very Nice article ! great job. Thanks for sharing.
Promote your business brands with bulk SMS marketing, contact us at +917404900081
Bulk SMS Service provider in UAE
Bulk sms service provider in Egypt
Bulk service provider Bahrain
Bulk SMS Service provider in U.S.
Bulk SMS Service provider in U.K.
Bulk sms service provider in Australia

Smith каза...

Thanks for this information, SMS Marketing is sending promotional campaigns or transactional messages for marketing purposes using text messages (SMS) How to send bulk sms

Rohit каза...

Your content is too good but why is your UI irritating me. rest of all is good thanks for sharing with us I have also written a blog please read and give the feedback

 
Vesko Kolev's Blog : IDeveloper -