0

Develop your own MSN java client.

Written By: Ivan Shi

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 packagefrom the offical project website, then unzip it to any folder on your HDD.Here I use d:java_devcommon,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 thepath where you place your msnmlib jar file. Click new… to add a new user library MSN_LIB and click Add Jars… to make the user library variable refer to msnmlib jar file:d:java_devcommonmsnm.jar

Now, edit properties of project jRCS by select the context menu of jRCS nodein 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 MSN_LIBhere.

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

Coding Step 1: Createyour own MsnAdapter classfor an adapter that can listen to the events firedby MSN Messenger.You should make youadapter class extend from theparent class rath.msnm.event.MsnAdapter. the MsnAdapter class has more the 10 methods for event listening. but you have not to implement all themethods, andby user EclipseNew Class Wizard, it is not ahard job to implement a lotofmethods.For short and easy, We only implementmethod MsnAdapter.instantMessageReceived(SwitchboardSession ss, MsnFriend friend, MimeMessage mime).
Method instantMessageReceived is invokedby the MsnMessengerinstance every time whenyou receive a new message(MimeMessage)from MsnFriend.
Here is my code to reply to thefriend”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.KIND_MESSAGE);

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 publicclasswith the static
main method as your own msn client.


public class Client {

public static void main(String[] args) {
Email = "your@hotmail.com";
Password = "you_msn_account_password";
// 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 theConsole.
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!

Previous post:

Next post: