Oct 5 2009

Send Instant Message Notifications from your .Net Applications!

Category: amilsark @ 21:44
 
I’ve been interested in figuring out how to notify someone of an interesting application event by other means than just email.
This led me to find the MSNPSharp .Net Instant Messaging Library.  This library works with the MSN Instant Messaging Protocol.
 
Here are some features of the current version (2.5.7 with 3.0 coming out Fall 2009) – borrowed from the MSNPSharp Google code site (http://code.google.com/p/msnp-sharp/)
 

Features

The library currently supports the following features:

Messaging
Address Book
Roaming Profile
Nameserver(NS) and Switchboard(SB)
  • Signing in from multiple places (MPOP)
  • Support for bots (Provisioned_Accounts)
  • Hotmail mailbox status
  • Notifications of new mail
  • Setting presence status
  • Request or receive conversations
  • Multiple users in one conversation
  • File transfers
  • P2P framework support
  • Flexible tracing of warnings and errors
  • Proxy support and custom servers
 
 
 

How I Use It:

 
I implemented a simple ‘SendIM’ method as part of a Notification class.  This will simply connect to the MSN service with my IM account credentials and then
send an instant message with whatever text we choose.   If you noticed in the features list of this library, it can do offline messaging.  I’m not sure exactly how that works yet,
but it is worth understanding a little more. 
 
 

Download the binaries from http://code.google.com/p/msnp-sharp/.
Add a reference in your project to the MSNPSharp library.
 
Follow the below code to create a one way Instant message to the email address ‘contact’ you pass in.  The account you use
must be a valid account with live.com or hotmail.
 
I’ve found that we need the Thread.Sleep  in order to give it time to connect or else a null reference exception is thrown on the
conv.Switchboard.SendTextMessage(message) line.
 
   1: public void SendIM(string contact)
   2:         {
   3:            var  messenger = new Messenger();  
   4:             var message = new TextMessage("This works!! Give Andy a dollar... Now please..");
   5:             messenger.Credentials.Account = "youraccountemailaddress";
   6:             messenger.Credentials.Password = "yourpassword";
   7:             messenger.Nameserver.BotMode = true;
   8:             messenger.Nameserver.AutoSynchronize = false;
   9:             messenger.Connect();
  10:             System.Threading.Thread.Sleep(5000);
  11:  
  12:             try
  13:             {
  14:                 var conv = messenger.CreateConversation();
  15:                 conv.Switchboard.Invite(contact);
  16:                 System.Threading.Thread.Sleep(5000);
  17:                 conv.Switchboard.SendTextMessage(message);
  18:             }
  19:             catch(NullReferenceException nullex)
  20:             {
  21:                  //fall back email or logging   
  22:             }
  23:             catch(Exception ex)
  24:             {
  25:                 //fall back email or logging
  26:             }
  27:  
  28:         }

 

If you have anymore experience with this library or Instant message protocol please comment and share any gotchas or ideas you might have that might help myself and the community.

Tags: , , , ,