📄 pop3session.java
字号:
package FirstProjects;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;
public class POP3Session extends Thread {
private static final String POP_USER = "USER";
private static final String POP_PASS = "PASS";
private static final String POP_STAT = "STAT";
private static final String POP_LIST = "LIST";
private static final String POP_RETR = "RETR";
private static final String POP_DELE = "DELE";
private final static String POP_QUIT = "QUIT";
private final static String POP_OK = "+OK";
private final static String POP_ERR = "-ERR";
private PrintWriter pw;
private BufferedReader bur;
private Socket socket;
public static final String USER = ("D:\\user.properties");
public static final String PATH = ("D:\\Mailsave");
private String userName;
private String usePass;
private File[] file;
public POP3Session(Socket socket) {
this.socket = socket;
}
public void sendMessage(String message) {
pw.println(message);
pw.flush();
}
public String readCommandLine() throws IOException {
while (true) {
String commandLine = bur.readLine();
if (commandLine != null) {
commandLine = commandLine.trim();
System.out.println(commandLine);
}
return commandLine;
}
}
public boolean sendWelcomeMessage() {
StringBuffer sb = new StringBuffer();
try {
sb.append(POP_OK).append(" ").append(InetAddress.getLocalHost())
.append(" ").append("(")
.append(Contants.VERSION_MAILSERVER).append(")")
.append(" ").append(Contants.POP_SERVER_NAME).append(
"\t" + Contants.WELCOME);
} catch (UnknownHostException e) {
e.printStackTrace();
}
sendMessage(sb.toString());
return true;
}
private boolean reciveCommand(String command) {
boolean returnValue = true;
if (command == null)
return false;
String argument = null;
int spaceIndex = command.indexOf(" ");
if (spaceIndex > 0) {
argument = command.substring(spaceIndex + 1);
command = command.substring(0, spaceIndex);
}
if (command.equalsIgnoreCase(POP_USER)) {
doUSER(argument);
} else if (command.equalsIgnoreCase(POP_PASS)) {
doPass(argument);
} else if (command.equalsIgnoreCase(POP_STAT)) {
doStat();
} else if (command.equalsIgnoreCase(POP_LIST)) {
doList(argument);
} else if (command.equalsIgnoreCase(POP_DELE)) {
doDele(argument);
} else if (command.equalsIgnoreCase(POP_RETR)) {
if (argument == null) {
sendMessage(POP_ERR + " Mail number or Password is required...");
} else
doRetr(argument);
} else if (command.equalsIgnoreCase(POP_QUIT)) {
doQuit();
return false;
}
return returnValue;
}
private void doUSER(String argument) {
StringBuffer responseBuffer = new StringBuffer();
userName = argument;
Properties T = new Properties();
try {
FileInputStream R = new FileInputStream(USER);
T.load(R);
usePass = T.getProperty(userName);
if (usePass == null) {
responseBuffer.append("-ERR");
System.out.println("Use Flase");
sendMessage(responseBuffer.toString() + "User can't Find!");
} else {
responseBuffer.append("+OK" +" "+ "core Mail");
sendMessage(responseBuffer.toString());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void doPass(String argument) {
String responsestring = null;
StringBuffer responseBuffer = new StringBuffer();
if (usePass.equals(argument)) {
responseBuffer.append(POP_OK);
sendMessage(responseBuffer.toString());
} else {
responseBuffer.append(POP_ERR);
sendMessage(responsestring);
}
}
private void doStat() {
String responsestring = null;
StringBuffer responseBuffer = new StringBuffer();
File fileMail = new File(PATH);
if (!fileMail.exists()) {
responsestring = "-ERR";
sendMessage(responsestring);
} else {
file = fileMail.listFiles();
long size = 0;
for (int i = 0; i < file.length; i++) {
size = size + file[i].length();
}
responseBuffer.append(POP_OK)
.append(" " + file.length + " " + size);
sendMessage(responseBuffer.toString());
}
}
private void doList(String argument) {
if (argument == null) {
sendMessage(POP_OK +" ");
int len = file.length;
for (int i = 0; i < len; i++)
sendMessage(i + 1 + " " + file[i].length());
sendMessage(".");
} else {
int index = Integer.parseInt(argument);
sendMessage(POP_OK);
sendMessage(index +" "+ file[index - 1].length());
}
}
private void doRetr(String argument) {
int index = Integer.parseInt(argument) - 1;
BufferedReader br;
try {
br = new BufferedReader(new FileReader(file[index]));
String s = null;
sendMessage(POP_OK + " ");
while ((s = br.readLine()) != null) {
sendMessage(s);
}
sendMessage(".");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(POP_RETR + POP_OK);
}
private void doDele(String argument) {
int index = Integer.parseInt(argument)-1;
File delefile = file[index];
if (delefile.delete()) {
String responsestring = "+OK";
StringBuffer sb = new StringBuffer();
sb.append(responsestring).append(" ").append("message ").append(
argument).append("deleted");
sendMessage(sb.toString());
} else {
String responsestring = "-ERR";
sendMessage(responsestring + " message " + argument + " already deleted");
}
}
private void doQuit() {
StringBuffer responseBuffer = new StringBuffer();
sendMessage(POP_OK);
responseBuffer.append(POP_OK + " " + "POP3 server close");
sendMessage(responseBuffer.toString());
}
public void run() {
try {
// socket.setSoTimeout(Contants.SOCKET_TIMEOUT);
bur = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
pw = new PrintWriter(socket.getOutputStream());
sendWelcomeMessage();
while (reciveCommand(readCommandLine())) {
// sendMessage(POP_ERR + " 错误信息");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bur.close();
pw.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -