Develop your own MSN java client.


Now, use msnmlib 1.2(the website is in Korean), you can write programs as client to MSN servers.
You can write msn robots or your own MSN Messenger use Java.

Here I have a very short tutorial on how to use msnmlib 1.2, and my Java development IDE is Eclipse 3.0

Prepare:If you had already downloaded msnmlib 1.2 package from the offical project website,  then unzip it to any folder on your HDD.Here I use d:javadevcommon ,you will get a jar file: msnm.jar.Otherwise, you have to download the latest version of msnmlib from the offical web site or the project page on sourceforge

First step, open your Eclipse IDE and use New Project wizard to create a new java project in your default workspace(ofcoz, you can use another workspace), and name your project(here I use jRCS,which means java Realtime Customer Servcie System)

Second, In Eclipse, Select menu Window> Prefence, expand the Java node in the left treeview,
and then expand Build path node in Java node, and then select node User Libraries. We will add a new user library refer to the path where you place your msnmlib jar file. Click new... to add a new user library MSNLIB and click Add Jars... to make the user library variable refer to msnmlib jar file:d:javadevcommonmsnm.jar

Now, edit properties of project jRCS by select the context menu of jRCS node in the Project Explorer. Here we will edit the Java Build Path and add a new library for this project. Of coz, we will add the pre-defined User Library MSNLIB here.

Ok, finally, we have our development environment set up. It's time for coding!

Coding Step 1: Create your own MsnAdapter class for an adapter that can listen to the events fired by MSN Messenger. You should make you adapter class extend from the parent class rath.msnm.event.MsnAdapter. the MsnAdapter class has more the 10 methods for event listening. but you have not to implement all the methods, and by user Eclipse New Class Wizard, it is not a hard job to implement a lot of methods. For short and easy, We only implement method MsnAdapter.instantMessageReceived(SwitchboardSession ss, MsnFriend friend,            MimeMessage mime).
Method instantMessageReceived is invoked by the MsnMessenger instance every time when you receive a new message (MimeMessage) from MsnFriend.
Here is my code to reply to the friend "How are you doing":


public class MSNAdapter extends MsnAdapter {

MSNMessenger msn;

public MSNAdapter(MSNMessenger msn) {
this.msn msn;
}

public void instantMessageReceived(SwitchboardSession ss,
MsnFriend friend, MimeMessage mime) {

System.out.println(friend.getFriendlyName()

+" send me some msg:"+mime.getMessage());

try

{

// create a new message

MimeMessage mm new MimeMessage();

// remember to append the

trail to

// your message

mm.setMessage("How are you doing?

");

// set the message kind to MESSAGE !!!

// you have to do this.

mm.setKind(mm.KINDMESSAGE);

if (robot.msn.sendMessage(mm,ss))

{

// message sent successfully

System.out.println("send msg ok!");

}else{

// failed

System.out.println("send msg faild!");

}

}catch(Exception e){

e.printStackTrace();

}

}
}



Coding Step 2: Write a public class with the static
main method as your own msn client.


public class Client {

public static void main(String[] args) {

Email "your@hotmail.com";

Password "youmsnaccountpassword";

// create a new MSNMessenger instance msn

msn new MSNMessenger(Email,Password);

// set the initial status to online.
msn.setInitialStatus(UserStatus.ONLINE);
// register your pre-created adapter to msn

msn.addMsnListener(new MSNAdapter(msn));

// login into the msn network

msn.login();

System.out.println("Waiting for the response....");

}
}



Coding Step 3: Debug or run your application.In the context menu
of project jRCS,  select Run > Java Application, Eclipse will compile the code
and run the application in the  Console.
If you have your msn account set properly, and you have another msn account that
add this msn account as contact, you will see this msn account log in, and then you can send
some message to this contact as you wish, how ever the Contact will only and always answer
you "How are you doing?".^

About the Author

Ivan Shi,
Web Server System Developer living and working in Shanghai,China
He has a Blog "Technically Blog Ivan"(http://www.ivanshi.com/), blogging his life,feel,mind and technology.
He is doing a lot to make his speaking and writing English much more better!