⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pop3session.java

📁 自己写的具有图形界面的SMTP和POP3邮件服务器端程序,实现邮件发送和收取,新建用户功能,用最基本的语句描述了邮件服务器端与客户端之间的通信过程
💻 JAVA
字号:
package cstest;

import java.io.OutputStreamWriter;
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 PATH = ("E:\\INBOX\\"); //邮件存储路径

      private String userName = null; //用户名

      private String usePass = null; //密码

      private File[] file;

      private UserInfoList uil = ServerTable.uil; //用户列表

      private int totalUser = uil.getCount(); //总用户数

      private static int userNumber; //当前用户序列号

      public Pop3Session(Socket socket) {
            this.socket = socket;
      }

      //消息发送方法,用于向管道流中写入对话内容
      public void sendMessage(String message) {
            System.out.println(message + "send");
            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("USER")) {
                  doUSER(argument);
            }
            else if (command.equalsIgnoreCase("PASS")) {
                  doPass(argument);
            }
            else if (command.equalsIgnoreCase("STAT")) {
                  doStat();
            }
            else if (command.equalsIgnoreCase("LIST")) {
                  doList(argument);
            }
            else if (command.equalsIgnoreCase("TOP")) {
                  doTop(argument);
            }
            else if (command.equalsIgnoreCase("DELE")) {
                  doDele(argument);
            }
            else if (command.equalsIgnoreCase("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) {
            boolean haveuser = false;
            StringBuffer responseBuffer = new StringBuffer();
            userName = argument;
            String temp[] = new String[totalUser];

            //依次读取数据库中的用户名,判断是否与当前输入的用户名相同
            for (int i = 0; i < totalUser; i++) {
                  temp[i] = uil.find(i).getUserName();
                  if (temp[i].equals(userName)) {
                        haveuser = true;
                        userNumber = i;
                        responseBuffer.append("+OK" + " " + "core Mail");
                        sendMessage(responseBuffer.toString());
                  }
            }
            if (!haveuser) {
                  responseBuffer.append(POP_ERR +
                                        "Sorry,the name is not exist!");
                  sendMessage(responseBuffer.toString());
            }
      }

      private void doPass(String argument) {
            String responsestring = null;
            StringBuffer responseBuffer = new StringBuffer();
            if (uil.find(userNumber).getPassword().equals(argument)) {
                  responseBuffer.append("+OK");
                  sendMessage(responseBuffer.toString());
            }
            else {
                  responseBuffer.append(POP_ERR +
                                        "Sorry,the password is wrong!");
                  sendMessage(responsestring.toString());
            }
      }

      private void doStat() {
            String responsestring = null;
            StringBuffer responseBuffer = new StringBuffer();
            File fileMail = new File(PATH + userName);
            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("+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 doTop(String argument) {
            int spaceIndex = argument.indexOf(" ");
            String messagem = argument.substring(0, spaceIndex);
            int m = Integer.parseInt(messagem) - 1;
            BufferedReader br;
            try {
                  br = new BufferedReader(new FileReader(file[m]));
                  String s = null;
                  sendMessage("+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 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 {
                  bur = new BufferedReader(new InputStreamReader(socket
                          .getInputStream()));
                  pw = new PrintWriter(new OutputStreamWriter(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 + -