KnowDotNet

SMS Messaging with Windows Mobile 5.0

by Bill Ryan

A while ago, I was playing around creating a bot, a Snarkier version of the Mobile Secretary Application . Well, it was late and I got sloppy. So basically, my little app sent out a text message to everyone on one of my company's Outlook distribution lists. I learned a lot that night.
So you've always been able to send SMS messages with Smartphone, but it's a lot easier now. Sending them isn't a big deal though. The cool thing is intercepting them. To that end, the
MessageInterceptor class makes it quite easy:
Create a module level variable (or whatever scope you need it).
      private MessageInterceptor Interceptor;

Instantiate it in the constructor
      Interceptor = new MessageInterceptor(InterceptionAction.NotifyAndDelete, false);

Specify a InterceptionAction property. (Notify or NotifyAndDelete) - those should be self explanatory [See above]
Specify if you want it to use the Form's thread (a must if you want to update the UI) [see above]
Wire up a
MessageInterceptorEventHandler.

      Interceptor.MessageReceived +=new MessageInterceptorEventHandler(Interceptor_MessageReceived);

Cast the e.Message object in the handler as a SmsMessage and use accordingly
void Interceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
{
SmsMessage IncomingMessage = (e.Message as SmsMessage);
String[] ImportantStuff = new String[]{"Dog pooping on carpet",
"Vista Launch",
"Would you care to explain THIS?",
"EDC"};
foreach(String s in ImportantStuff){
if(IncomingMessage.Body.IndexOf(s)> -1){
       SmsMessage OutgoingMessage = new SmsMessage(e.Message.From.Address, "This is the Cuckoobot, I'm handling Bill's       messages, he'll be in touch shortly");
       OutgoingMessage.Send();
//Yes, I know this isn't optimal - it's demo code.
}
else{
         SmsMessage OutgoingMessage = new SmsMessage(e.Message.From.Address, "This is the Cuckoobot. Your message isn't  important and will have to wait.");
         OutgoingMessage.Send();
}
}
}