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

📄 serverthread.java

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

import java.net.*;
import java.io.*;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;

public class ServerThread
        extends Thread {
      private Socket socket;
      private BufferedReader re;
      private PrintWriter send;
      private String n = "\r\n";
      private String recieve;
      private String MAIL_FROM;
      private String MAIL_TO;
      private final static String MAIL_PATH = "E:\\swilenly";
      public static final int MAX_LENGTH = 1024;

      public ServerThread() {

      }

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

      public void run() {
            try {
                  re = new BufferedReader(new InputStreamReader(
                          socket.getInputStream()));
                  send = new PrintWriter(new
                                         OutputStreamWriter(socket.
                          getOutputStream()));
                  send.print("220" +
                             " Welecom to ken's SMTP Server version 0.1\n");
                  send.flush();
                  while (parseCommand(read())) {
                  }
                  recieve = re.readLine();
                  SmtpFrame.textarea.append(recieve + "\n");
            }

            catch (Exception ex) {

            }
            /*     finally {
                            try {
                                    re.close();
                                    send.close();
                                    socket.close();
                            } catch (IOException e) {
                                    e.printStackTrace();
                            }
                    }   */

      }

      private boolean parseCommand(String command) throws IOException {
            boolean flag = true;
            if (command == "") {
                  return false;
            }
            System.out.println(command);
            String argument = null;
            int spaceIndex = command.indexOf(" ");
            if (spaceIndex > 0) {
                  argument = command.substring(spaceIndex + 1);
                  command = command.substring(0, spaceIndex);

            }

            if (command.equalsIgnoreCase("mail")) {
                  doMAIL(argument);
            }
            else if (command.equalsIgnoreCase("ehlo")) {

                  doHELO(argument);
            }
            else if (command.equalsIgnoreCase("rcpt")) {

                  doRCPT(argument);
            }
            else if (command.equalsIgnoreCase("data")) {

                  doDATA(argument);
            }
            else if (command.equalsIgnoreCase("quit")) {
                  doQUIT(argument);
                  flag = false;
            }
            return flag;
      }

      public void doHELO(String argument) {
            StringBuffer responseBuffer = null;
            responseBuffer = new StringBuffer();
            if (argument == null) {
                  responseBuffer.append("501 Domain address required: ").append(
                          "helo");
            }
            else {
                  responseBuffer.append("250").append(" Hello ").append(
                          argument)
                          .append(" (").append(socket.getRemoteSocketAddress())
                          .append(")");
            }

            send.println(responseBuffer.toString());
            send.flush();
            return;
      }

      private void doMAIL(String argument) {
            StringBuffer responseBuffer = null;
            responseBuffer = new StringBuffer();
            if (argument == null) {
                  responseBuffer.append("501 Domain address required: ").append(
                          "rcpe");
            }
            else {
                  MAIL_FROM = argument.substring(6, argument.indexOf("@"));
                  SmtpFrame.textarea.append(MAIL_FROM + " login" + "\n");
                  responseBuffer.append("250").append(" Ok ");
            }
            send.println(responseBuffer.toString());
            send.flush();
      }

      private void doRCPT(String argument) {

            StringBuffer responseBuffer = new StringBuffer();
            if (argument == null) {
                  responseBuffer.append("501 Domain address required: ").append(
                          "mail");
            }
            else {
                  MAIL_TO = argument.substring(4, argument.indexOf("@"));
                  responseBuffer.append("250").append(" Ok ");

            }

            send.println(responseBuffer.toString());
            send.flush();
      }

      private void doDATA(String argument) {

            StringBuffer responseBuffer = new StringBuffer();
            StringBuffer responseBuffer2 = new StringBuffer();

            responseBuffer.append("354").append(" Ok ");

            send.println(responseBuffer.toString());
            send.flush();
            try {
                  sendMail();

                  responseBuffer2.append("250").append(" Message ").append(
                          "queued");
                  send.println(responseBuffer2.toString());
                  send.flush();

            }
            catch (IOException e) {

                  e.printStackTrace();
            }
      }

      private void sendMail() throws IOException {
            String buffer;
            Date date = new Date();
            long intTime = date.getTime();
            PrintWriter sout = new PrintWriter(new FileOutputStream(
                    "E:\\INBOX\\" + MAIL_TO + "\\" + intTime + ".txt"));
            while (true) {
                  buffer = re.readLine();
                  if (buffer.equalsIgnoreCase(".")) {
                        SmtpFrame.textarea.append(MAIL_FROM +
                                                  " send a message to " +
                                                  MAIL_TO
                                                  + " successfully " +
                                                  date.toString());
                        break;
                  }
                  sout.write(buffer);

                  sout.println();

                  sout.flush();
            }

            sout.close();

      }

      private void doQUIT(String argument) throws IOException {

            StringBuffer responseBuffer = new StringBuffer();

            responseBuffer.append("221").append(" Goodbye ");

            send.close();
            re.close();
            socket.close();

            send.println(responseBuffer.toString());
            send.flush();
            socket.close();
      }

      public static void server() {

            System.out.println("smtp:" + Thread.currentThread().getName());
            try {

                  ServerSocket ss = new ServerSocket(25);
                  SmtpFrame.textarea.append("SMTP server start at:  " +
                                            InetAddress.getLocalHost() +
                                            "  port: 25 \n");

                  while (true) {
                        Socket socket = ss.accept();
                        new ServerThread(socket).start();
                  }

            }
            catch (Exception ex) {
                  ex.printStackTrace();
                  //Pop3Server pop=new Pop3Server();
                  // pop.start();
            }
      }

      final String read() {
            for (; ; ) {
                  String commandLine = "";
                  try {
                        commandLine = re.readLine();
                  }
                  catch (IOException e) {

                        e.printStackTrace();
                  }
                  if (commandLine != null) {
                        commandLine = commandLine.trim();
                        return commandLine;
                  }
            }
      };

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -