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

📄 pop3client.java

📁 mywork是rcp开发的很好的例子
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			try {
				Runtime runtime = Runtime.getRuntime();
				Process process = runtime.exec(cmd);
				process.waitFor();
				debugOut("Preconnect command done.");
			} catch (InterruptedException ie) {
				throw new InterruptedIOException(ie.getMessage());
			}
		}
		if(ssl){
			socket=SSLSocketFactory.getDefault().createSocket(getHost(), (getPort() == -1) ? DEFAULT_POP3_PORT : getPort());
		}else{
			socket = new Socket(getHost(), (getPort() == -1) ? DEFAULT_POP3_PORT : getPort());
		}
		socket.setSoTimeout(getTimeout());
		in = new BufferedInputStream(socket.getInputStream());
		out = new BufferedOutputStream(socket.getOutputStream());
		if (responseBytes == null) {
			responseBytes = new byte[MAX_LINE_LENGTH];
		}
		status = readResponseLine();
		greeting = status;
		connected = true;
		debugOut("S:" + greeting);
	}
	
	/**
	 * ꆼꆼꆼꆼꆼTCP/IPꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @exception java.io.IOException	IOꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	protected synchronized void closeConnection() throws IOException {
		try {
			socket.close();
		} finally {
			socket = null;
			in = null;
			out = null;
			status = null;
			greeting = null;
			connected = false;
		}
	}

	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @param	command	ꆼꆼꆼꆼꆼꆼꆼ.
	 * @return	ꆼꆼꆼꆼꆼꆼꆼOKꆼꆼꆼꆼꆼ true. ꆼꆼꆼꆼꆼꆼꆼ false.
	 * @exception java.io.IOException IOꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	protected synchronized boolean sendCommand(String command) throws IOException {
		// ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
		if (messageInput != null) {
			messageInput.close();
			messageInput = null;
		}
		debugOut("C:" + command);
		byte data[] = command.getBytes(ENCODE);
		out.write(data, 0, data.length);
		out.write(CR);
		out.write(LF);
		out.flush();
		status = readResponseLine();
		debugOut("S:" + status);
		return isStatusOK();
	}

	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼ LIST ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.<BR>
	 *
	 * @exception	jp.gr.java_conf.roadster.net.pop.POP3NegativeResponseException	LISTꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * @exception	java.io.IOException	IOꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	protected synchronized void initList() throws POP3NegativeResponseException, IOException {
		if (sendCommand(COMMAND_LIST)) {
			Vector tmplist = new Vector();
			String line = readResponseLine();
			while (! ".".equals(line)) {
				tmplist.addElement(line);
				line = readResponseLine();
			}
			messageList = new String[tmplist.size()];
			tmplist.copyInto(messageList);
		} else {
			messageList = null;
			throw new POP3NegativeResponseException(getStatusString());
		}
	}
	
	/**
	 * UID(unique-id)ꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼ UIDL ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.<BR>
	 *
	 * @exception	jp.gr.java_conf.roadster.net.pop.POP3NegativeResponseException	LISTꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * @exception	java.io.IOException	IOꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	protected synchronized void initUIDList() throws POP3NegativeResponseException, IOException {
		if (sendCommand(COMMAND_UIDL)) {
			Vector tmplist = new Vector();
			String line = readResponseLine();
			while (! ".".equals(line)) {
				tmplist.addElement(line);
				line = readResponseLine();
			}
			uidList = new String[tmplist.size()];
			tmplist.copyInto(uidList);
		} else {
			uidList = null;
			throw new POP3NegativeResponseException(getStatusString());
		}
	}

	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ LIST ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.<BR>
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * <UL>
	 *   3  4231<BR>
	 * </UL>
	 *
	 * @param	pos	LISTꆼꆼꆼꆼ.
	 * @return	ꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * @exception	java.io.IOException	IOꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	protected synchronized String getListItem(int pos) throws IOException {
		if (messageList == null) {
			initList();
		}
		return new String(messageList[pos]);
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ LIST ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.<BR>
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * <UL>
	 *   <CODE>
	 *   String ret[] = client.getListItems();<BR>
	 *   ret[0] -> "1 23435"<BR>
	 *   ret[1] -> "2 4352"<BR>
	 *   ret[2] -> "3 45454"<BR>
	 *   </CODE>
	 * </UL>
	 *
	 * @return	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * @exception	java.io.IOException	IOꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	protected synchronized String[] getListItems() throws IOException {
		if (messageList == null) {
			initList();
		}
		String ret[] = new String[messageList.length];
		for (int i = 0; i < ret.length; ++i) {
			ret[i] = new String(messageList[i]);
		}
		return ret;
	}

	/**
	 * POP ꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼIllegalStateExceptionꆼthrowꆼꆼꆼ.
	 *
	 * @param	user	ꆼꆼꆼꆼ.
	 * @exception	java.lang.IllegalStateException	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	public synchronized void setUser(String user) {
		if (isConnected()) {
			throw new IllegalStateException("Already connected.");
		}
		this.user = user;
	}
	
	/**
	 * POP ꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @return	POPꆼꆼꆼꆼ.
	 */
	public synchronized String getUser() {
		return user;
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * nullꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @param	password	ꆼꆼꆼꆼꆼ.
	 * @exception	java.lang.IllegalStateException	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	public synchronized void setPassword(String password) {
		if (isConnected()) {
			throw new IllegalStateException("Already connected.");
		}
		if (password == null) {
			password = "";
		}
		this.password = password;
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @return	ꆼꆼꆼꆼꆼ.
	 */
	public synchronized String getPassword() {
		return password;
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @param	mode	ꆼꆼꆼꆼꆼ.<BR>
	 *                  NORMAL_AUTHORIZATION ꆼꆼꆼ APOP_AUTHORIZATION ꆼꆼꆼꆼꆼ.
	 */
	public void setAuthorizationMode(int mode) {
		authorizationMode = mode;
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @return	ꆼꆼꆼꆼꆼ. NORMAL_AUTHORIZATION ꆼꆼꆼ APOP_AUTHORIZATION.
	 */
	public int getAuthorizationMode() {
		return authorizationMode;
	}
	
	/**
	 * POPꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @param	host	POPꆼꆼꆼꆼꆼꆼꆼ.
	 * @exception	java.lang.IllegalArgumentException	host ꆼ null ꆼꆼꆼ.
	 * @exception	java.lang.IllegalStateException	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	public void setHost(String host) {
		if (host == null) {
			throw new IllegalArgumentException("host == null.");
		}
		if (isConnected()) {
			throw new IllegalStateException("Already connected.");
		}
		this.host = host;
	}
	
	/**
	 * POPꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @return	POPꆼꆼꆼꆼꆼꆼꆼ.
	 */
	public synchronized String getHost() {
		return host;
	}
	
	/**
	 * POPꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @param	port	POPꆼꆼꆼꆼꆼꆼꆼꆼ. -1 ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * @exception	java.lang.IllegalArgumentException	port < -1 ꆼꆼꆼ.
	 * @exception	java.lang.IllegalStateException	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	public synchronized void setPort(int port) {
		if (port < -1) {
			throw new IllegalArgumentException("port < -1.");
		}
		if (isConnected()) {
			throw new IllegalStateException("Already connected.");
		}
		this.port = port;
	}

	/**
	 * POPꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @return	POPꆼꆼꆼꆼꆼꆼꆼꆼ. -1 ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	public synchronized int getPort() {
		return port;
	}
	
	/**
	 * greeting(ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ)ꆼꆼꆼ.
	 * isConnected() == true ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * ꆼꆼꆼꆼꆼꆼ APOP ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ digest ꆼꆼꆼꆼꆼ.
	 *
	 * @return	greeting.
	 */
	protected String getGreeting() {
		return greeting;
	}
	
	/**
	 * APOP ꆼ digest ꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @return	digest ꆼꆼꆼ.
	 */
	private String createDigest() {
		StringBuffer buff = new StringBuffer();
		try {
			String key = "";
			String g = getGreeting();
			int spos = g.indexOf('<');
			int epos = g.indexOf('>');
			if (spos != -1 && epos != -1 && spos < epos) {
				key = g.substring(spos, epos + 1);
			}
			key = key + getPassword();
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] digest = md.digest(key.getBytes(ENCODE));
			for (int i = 0; i < digest.length; ++i) {
				char c = (char) ((digest[i] >> 4) & 0xf);
				if (c > 9) {
					c = (char) ((c - 10) + 'a');
				} else {
					c = (char) (c + '0');
				}
				buff.append(c);
				
				c = (char) (digest[i] & 0xf);
				if (c > 9) {
				    c = (char)((c - 10) + 'a');
				} else {
				    c = (char)(c + '0');
				}
				buff.append(c);
			}
		} catch (NoSuchAlgorithmException nsae) {
			nsae.printStackTrace();
		} catch (UnsupportedEncodingException uee) {
			uee.printStackTrace();
		}
		return buff.toString();
	}

	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * TCP/IPꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ
	 * ꆼꆼꆼꆼꆼꆼ.
	 *
	 * @exception	POP3AuthenticationFailedException	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * @exception	java.io.IOException	IOꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * @exception	java.lang.IllegalStateException	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	public synchronized void connect() throws POP3AuthenticationFailedException, IOException {
		openConnection();
		boolean authorized = false;
		if (isStatusOK()) {
			if (getAuthorizationMode() == APOP_AUTHORIZATION) {
				String digest = createDigest();
				if (sendCommand(COMMAND_APOP + ' ' + getUser() + ' ' + digest)) {
					authorized = true;
				}
			} else {
				if (sendCommand(COMMAND_USER + ' ' + getUser()) && sendCommand(COMMAND_PASS + ' ' + getPassword())) {
					authorized = true;
				}
			}
		}
		if (! authorized) {
			String stat = getStatusString();
			closeConnection();
			throw new POP3AuthenticationFailedException(stat);
		}
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * QUITꆼꆼꆼꆼꆼꆼꆼꆼꆼTCP/IPꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @exception	jp.gr.java_conf.roadster.net.pop.POP3Exception	POP3ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 * @exception	java.io.IOException	IOꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 */
	public synchronized void disconnect() throws POP3Exception, IOException {
		if (! isConnected()) {
			return;
		}
		try {
			if (sendCommand(COMMAND_QUIT)) {
				closeConnection();
			} else {
				throw new POP3NegativeResponseException(getStatusString());
			}
		} finally {
			messageList = null;
			uidList = null;
		}
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @return	ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼtrue.
	 */
	public synchronized boolean isConnected() {
		return connected;
	}

	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @return	ꆼꆼꆼꆼꆼꆼ.
	 */
	public synchronized String getStatusString() {
		return status;
	}
	
	/**
	 * ꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼꆼ.
	 *
	 * @return	OKꆼꆼꆼꆼꆼ true.
	 */
	public synchronized boolean isStatusOK() {
		if (status != null && status.startsWith(RESPONSE_OK)) {
			return true;
		}
		return false;
	}
	
	/**
	 * ꆼꆼꆼꆼ STAT ꆼꆼꆼꆼꆼꆼꆼꆼꆼ.

⌨️ 快捷键说明

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