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

📄 smtpsession.java

📁 自己写的JAVA 邮件服务器..单机和连www.163.com可以..POP3可以用OUTLOOK连,SMTP 还不行
💻 JAVA
字号:
package FirstProjects;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.FileOutputStream;


public class SMTPSession extends Thread {

	private static final String GET_HELO = "HELO";

	private static final String GET_MAIL = "MAIL";

	private static final String GET_RCPT = "RCPT";

	private static final String GET_DATA = "DATA";

	private final static String GET_QUIT = "QUIT";
	
	private static final String GET_DOT = ".";

	public String dir;

	private String path = "d:\\mail\\";

	public File from;

	public String to;

	// public File user = new File(path + "user.txt");

	private Socket socket;

	private BufferedReader br;

	private PrintWriter pw;

	private File f1;

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

	public boolean sendWelcomeMessage() {
		StringBuffer sb = new StringBuffer();
		try {
			sb.append("220").append(" ").append(InetAddress.getLocalHost())
					.append(" ").append("(")
					.append(Contants.VERSION_MAILSERVER).append(")")
					.append(" ").append(Contants.SMTP_SERVER_NAME).append(
							"\t" + Contants.WELCOME);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		sendMessage(sb.toString());
		return true;
	}

	public void sendMessage(String message) {
		pw.println(message);
		pw.flush();
	}

	final String readCommandLine() throws IOException {
		while (true) {
			String commandLine = br.readLine();
			if (commandLine != null) {
				commandLine = commandLine.trim();
			}
			return commandLine;
		}
	}

	private boolean parseCommand(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(GET_HELO))
			doHELO(argument);

		else if (command.equalsIgnoreCase(GET_MAIL)) {
			doMail(argument);
		}

		if (command.equalsIgnoreCase(GET_RCPT))
			doRCPT(argument);

		else if (command.equalsIgnoreCase(GET_DATA)) {
			doData(argument);
		}

		else if (command.equalsIgnoreCase(GET_QUIT)) {
			doQuit(argument);
			returnValue = false;
			try {
				if (compare()) {
					copyFile();
				}
				File send = new File(path + dir + "\\send.txt");
				send.delete();
		        f1.delete();
				File file = new File(path + dir + "\\");
				file.delete();
				System.out.println("quit ok!");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return returnValue;
	}

	private void doHELO(String argument) {
		StringBuffer responseBuffer = new StringBuffer();
		responseBuffer.append("250 Welcome!").append(" ").append(argument);
		sendMessage(responseBuffer.toString());
	}

	public void doMail(String argument) {
		StringBuffer responseBuffer = new StringBuffer();
		if(argument==null){
			sendMessage("501 the wrong argument");
		}
		int index1 = argument.indexOf("<")+1;
		int index2 = argument.indexOf("@");
		int index3 = argument.indexOf(">");
		dir = argument.substring(index1 + 1, index2); // 取出<@之间的
		to = argument.substring(index1 + 1, index3); // 取出<>之间的
		from = new File(path + createDir() + "\\" + dir + ".txt");
		if (!from.exists())
			try {
				from.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		responseBuffer.append("250 OK!");
		sendMessage(responseBuffer.toString());
		System.out.println(GET_MAIL);
	}

	public String createDir() {
		File mdir = new File("d:\\mail\\" + dir);
		mdir.mkdir();
		return dir;
	}

	private void doRCPT(String argument) {
		StringBuffer responseBuffer = new StringBuffer();
		if(argument==null){
			sendMessage("501 the wrong argument");
		}
		int index1 = argument.indexOf("<")+1;
		int index2 = argument.indexOf("@");
		int index3 = argument.indexOf(">");
		dir = argument.substring(index1 , index2);
		to = argument.substring(index1 , index3);
		from = new File(path + createDir() + "\\" + dir + ".txt");
		if (!from.exists())
			try {
				from.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		responseBuffer.append("250 OK!");
		sendMessage(responseBuffer.toString());
		System.out.println(GET_RCPT);
	}

	private boolean writeDATA(String data, PrintStream p) {
		boolean returnValue = true;
		if (data.equalsIgnoreCase(".")) {
			returnValue = false;
		} else {
			p.println(data);
		}
		return returnValue;
	}

	private void doData(String argument) {
		sendMessage("354 Start mail input");
		String responsestring = "+OK";
		StringBuffer sb = new StringBuffer();
		sb.append(responsestring).append("\n").append("Subject: mailmessage");
		sendMessage(sb.toString());
		from = new File(path + createDir() + "\\" + dir + ".txt");
		try {
			br = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			FileOutputStream out;
			out = new FileOutputStream(from);
			PrintStream p = new PrintStream(out);
			while (writeDATA(readCommandLine(), p)) {
			}
			p.close();
			out.close();
			sendMessage(responsestring + "\t" + "END");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("250");
		}
	}

	private void doQuit(String argument) {
		StringBuffer responseBuffer = new StringBuffer();
		responseBuffer.append("250 OK ").append("GET_QUIT").append("\tOK")
				.append("\tGoodBye!");
		sendMessage(responseBuffer.toString());
		// try {
		// br.close();
		// pw.close();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
	}

	public void run() {
		try {
			socket.setSoTimeout(Contants.SOCKET_TIMEOUT);
			br = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			pw = new PrintWriter(socket.getOutputStream());
			sendWelcomeMessage();
            while (parseCommand(readCommandLine())) {
				//				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				br.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 + -