网上也有不少例子,但是有些朋友遇到所谓911错误导致无法登录,isLogin=false。我对原有的代码进行了一点修改加了个等待10秒,因为发现每次登入耗时都不等,同时附上.jar包,因为个人使用中出现jar包问题导致无法登入. 修改代码如下:
package com.msn;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import rath.msnm.BuddyGroup;
import rath.msnm.BuddyList;
import rath.msnm.GroupList;
import rath.msnm.MSNMessenger;
import rath.msnm.msg.MimeMessage;
/**
* Created by IntelliJ IDEA.
* User: Eric
* Date: 2008-12-2
* Time: 14:33:20
* To change this template use File | Settings | File Templates.
*/
public class MsnFriendOut {
/**
* getGroups(String userName,String password) 查询所有的 msn 分组。
* getBuddyList(String userName,String password) 查询所有好友.(其他查询在线好友也雷同).
*
* @param args
*/
private static final Logger log = Logger.getLogger(MsnFriendOut.class);
private MSNMessenger msn = null;
public static void main(String[] args) {
MsnFriendOut ms = new MsnFriendOut();
String msnAddress = JOptionPane.showInputDialog("请输入你的MSN");
String password = JOptionPane.showInputDialog("请输入你的密码");
ms.login(msnAddress, password);
List<String> buddyList = ms.getBuddyList(msnAddress, password);
if (buddyList != null) {
for (String name : buddyList) {
System.out.println("Buddy :" + name);
}
}
}
public void setMsnMessager(String userName, String password) {
this.msn = new MSNMessenger(userName, password);
}
public MSNMessenger getMsnMessager() {
return msn;
}
/**
* Login ur msn base on username and password.
*
* @param userName
* @param password
*/
public void login(String userName, String password) {
if (msn == null)
setMsnMessager(userName, password);
try{
msn.login();
}catch(Exception e){
log.error(e);
}
}
/**
* Get all of ur msn group by username and password. (推荐)
*
* @param userName
* @param password
* @return all ur msn group .
*/
public List<String> getGroups(String userName, String password) {
if (msn == null)
setMsnMessager(userName, password);
if (!msn.isLoggedIn()) {
login(userName, password);
}
return getGroups(msn);
}
/**
* Get all of ur buddy by username and password. (推荐)
*
* @param userName
* @param password
* @return All the buddy
*/
public List<String> getBuddyList(String userName, String password) {
if (msn == null)
setMsnMessager(userName, password);
login(userName, password);
while(!msn.isLoggedIn()){
login(userName, password);
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("isLogin:"+ msn.isLoggedIn());
}
return getBuddyList(msn);
}
/**
* Get all of ur buddy group by MSNMessage.(不推荐)
*
* @param msn
* @return
*/
public List<String> getGroups(MSNMessenger msn) {
if (msn == null)
return null;
if (!msn.isLoggedIn()) {
log.warn("Please set the MSNMessge");
msn.login();
}
BuddyGroup buddyGroup = msn.getBuddyGroup();
GroupList groupList = buddyGroup.getGroupList();
Iterator groupIterm = groupList.iterator();
List list = null;
while (groupIterm.hasNext()) {
list = (list == null) ? new ArrayList() : list;
list.add(groupIterm.next().toString());
}
return list;
}
/**
* Get all of ur buddy by MSNMessage.(不推荐,)
*
* @param msn
* @return
*/
public List<String> getBuddyList(MSNMessenger msn) {
if (msn == null) {
log.warn("Please set the MSNMessge");
return null;
}
if (!msn.isLoggedIn()) {
msn.login();
return null;
}else{
BuddyGroup buddyGroup = msn.getBuddyGroup();
BuddyList buddyList = buddyGroup.getForwardList();
List list = null;
// check.
if (buddyList == null || buddyList.size() < 1)
return null;
for (int i = 0; i < buddyList.size(); i++) {
list = (list == null) ? new ArrayList() : list;
String fname = buddyList.get(i).getLoginName();
list.add(fname);
}
MimeMessage msg = new MimeMessage();
msg.setMessage("测试");
try {
boolean sendOk = msn.sendMessage("*****@hotmail.com", msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
}