现在公司要用James来做邮件服务器,但要求我们用Java写出客户端和服务端的管理小软件,
Web客户端功能包括收件箱、发邮件、发件箱、草稿箱、回收站、地址本、自定义文件夹、配置等。
服务端的管理主要包括如下功能:后台管理的功能主要包括用户的添加、删除、修改、用户使用空间指配、邮件备份等。
请问该如何来实现?
特别是在web端收发,带附件的邮件,该如何来做JavaMail收发带附件的程序代码我都会写,但是如何将附件上传到web页面,然后发送出去,该怎么做?
客户端主要是这个难点。
最关键的是服务端,不知道如何利用james来在程序中实现用户的添加,修改等,不知道James有没有提供这样的API,请高手指教,万分感谢,十万火急。
james不直接提供添加删除用户的api,但是你可以改造数据库结构自己自己对数据库操作啊。
最难的是自定义文件夹、配置,我还没有实现,虽然有思路,但不想写代码:(
去找james的文档来看吧,虽然比较痛苦,我就是这样过来的。
可以用rmi调用james服务器程序实现用户的添加,修改等...
详见\james-2.2.0-src\proposals\rmi-remotemanager
jmx方式好象还没实现吧,不懂。
小弟已经写完,谢谢leozmy(麦庄) 的提醒,直接通过远程调用服务器上的命令行来实验,贴出来大家共享一下
package com.hypersoft.ccm.util.mail;
imp
imp
imp
imp
imp
imp
imp
/**
* @author wangjb
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Co
*/
public class MailServerMng {
private String serverAddress = "localhost";
private int port = 4555;
private String userId = "root"; // administrators userid
private String password = "root";
private Socket connectionSocket = null;
private PrintStream out = System.err;
private BufferedReader in = null;
private boolean connectedStatus;
public MailServerMng() {
}
public MailServerMng(String server, int port, String uid, String pwd) {
this.userId = uid;
this.password = pwd;
this.serverAddress = server;
this.port = port;
this.connectedStatus = connect();
}
//methods
public void ensure(String _userid, String _password, String _domain)
throws IOException {
if (serverAddress.equalsIgnoreCase(_domain)) {
if (!isConnected()) {
connect();
}
if (verifyUser(_userid)) {
// user exists in james and changing password
if (null != _password && 0 < _password.length())
changePassword(_userid, _password);
} else
addUser(_userid, _password);
// user was not in james and should
} else {
if (!isConnected()) {
connect();
}
if (verifyUser(_userid)) { // user exists in James and should not
delUser(_userid);
}
}
}
public boolean isConnected() {
return this.connectedStatus;
}
private void disconnect() {
try {
this.out.println("quit");
this.out.close();
this.in.close();
this.connectionSocket.close();
this.connectedStatus = false;
return;
} catch (IOException e) {
this.connectedStatus = false;
log("Couldn't get I/O for " + "the connection to server.");
return;
}
}
public boolean connect() {
try {
this.connectionSocket = new Socket(this.serverAddress, this.port);
this.out =
new PrintStream(connectionSocket.getOutputStream(), true);
this.in =
new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));
//Login to the server
String userInput;
log(in.readLine());
//wait for the connection
out.println(this.userId);
//send userid
log(in.readLine());
//wait for \n
out.println(this.password);
//send the password
//log (in.readLine());
if (in.readLine().indexOf("HELP") != -1) {
this.connectedStatus = true;
log("Server accepted connection");
return true;
} else {
this.connectedStatus = false;
log("Server refused connection");
return false;
}
} catch (UnknownHostException e) {
this.connectedStatus = false;
log("Unable to connect to the host!");
return false;
} catch (IOException e) {
System.err.println("erreur officiel : \n" + e);
this.connectedStatus = false;
log("Couldn't get I/O for " + "the connection to server.");
return false;
}
}
private String execute(String command) throws IOException {
String serverSays;
if (connectedStatus == false)
connect();
// now execute the command
log("executing: -" + command + "-");
out.println(command);
out.println("###");
String returnString = "";
char[] inChars = new char[1];
serverSays = in.readLine();
while (serverSays != null && serverSays.endsWith("###") == false) {
returnString += serverSays + "\n";
serverSays = in.readLine();
}
// log("returning"+returnString);
return returnString;
}
public boolean verifyUser(String userName) throws IOException {
if (!isConnected())
connect();
if (!isConnected())
log("broken"); // do something
String response = execute("verify " + userName);
// verify if specified user exist
boolean exists = false;
if (null != response && -1 == response.indexOf("does not"))
exists = true;
return exists;
}
public boolean addUser(String userName, String password)
throws IOException {
if (!isConnected())
connect();
if (!isConnected())
log("broken: no connection to James"); // do something
String response = execute("adduser " + userName + " " + password);
boolean exists = false;
System.err.println("reponse : " + response);
if (null != response) {
if (0 <= response.indexOf("added")) {
exists = true;
} else if (0 <= response.indexOf("already")) {
exists = false;
}
}
return exists;
}
public boolean delUser(String _userName, String _domain)
throws IOException {
if (serverAddress.equalsIgnoreCase(_domain)) {
return delUser(_userName);
}
return true; // well, he is not here!
}
public boolean delUser(String userName) throws IOException {
if (!isConnected())
connect();
if (!isConnected())
log("broken"); // do something
boolean exists = false;
String response = execute("deluser " + userName + " " + password);
if (null != response) {
} else if (0 <= response.indexOf("deleted")) {
exists = true; // was deleted
} else if (0 <= response.indexOf("Error")) {
exists = true; // didn't exist
}
return exists;
}
public boolean setPassword(String userName, String password)
throws IOException {
if (!isConnected())
connect();
if (!isConnected())
log("broken"); // do something
boolean exists = false;
String response = execute("setpassword " + userName + " " + password);
if (null != response) {
} else if (0 <= response.indexOf("reset")) {
exists = true; // was deleted
} else if (0 <= response.indexOf("Error")) {
exists = true; // didn't exist
}
return exists;
}
public boolean changePassword(String userName, String password)
throws IOException {
//if (verifyUser(userName)) // james 2 added setpassword
// delUser(userName);
return setPassword(userName, password);
}
private void log(String note) {
System.err.println(getClass().getName() + " " + note);
}
}