大多数应用将邮件发送至传统的邮件客户端工具,比如Outlook和Hotmail。但是,用户是可以有多种联系方式的。有些用户喜欢通过实时消息客户端工具MSN
Messenger来接收邮件消息。
使用MSNj使得应用能够支持MSNMessenger并不难做到。MSNj实现了java的MSN Messenger协议。SourceFORGE负责开发,提供了简单的API,这些API能够发送和接收消息。
MSNj除了能够收发消息外,还能够对MSNMessenger上的完全开放用户提供保护。这项功能的设置是通过UserState这个类完成的。一般来说,用户状态包括:暂时离开,立即回来,离线和空闲。
MSNj还包括了一个ConnectionListener接口。它允许应用修改用户列表。比如,当另有用户将当前用户加入其转发列表,并且需要被放进许可列表或者禁止列表时,MSNj能够作出标记。
以下列出MSNj的程序示例:
import org.gimec.msnj.Connection;
import org.gimec.msnj.ConnectionException;
import org.gimec.msnj.MimeMessage;
public class MessengerSend {
static public void main(String[] args) {
// your messenger username andpassword
String username ="...";
String password ="...";
// the username of the personyou wish to message
String recipient ="...";
try {
Connectionconn = new Connection();
conn.login(username,password);
MimeMessagemsg = new MimeMessage("Hello "+recipient);
conn.sendMessage(recipient,msg);
conn.logout();
} catch(ConnectionException ce) {
if(ce.getErrorCode()!= null) {
System.err.println("Messenger Error Code: "+ce.getErrorCode());
}
ce.printStackTrace();
}
}
}
更多信息或要下载MSNj,请参见SourceFORGE.