📄 pop3.java~38~
字号:
package pop3;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author not attributable * @version 1.0 */import java.io.*;import java.net.*;import java.util.*;public class pop3 { protected int port=110; protected String hostname="localhost"; protected String password=""; protected String username=""; protected Socket socket; protected BufferedReader br; protected PrintWriter pw; public pop3() throws Exception{ try { getInput(); //获得用户输入 displayEmails(); //获得邮件信息 } catch(Exception e) //出错处理 { System.err.println("error occured -details follow"); e.printStackTrace(); System.out.println(e.getMessage()); } } protected boolean responseIsOK() throws Exception //如果pop相应就返回true,否则返回false { String line=br.readLine(); //获得server的反应 System.out.println("<"+line); return line.toUpperCase().startsWith("+OK"); } protected String readline(boolean debug) throws Exception //从server读,并且显示 { String line=br.readLine(); //加入<表示是server的控制反应 if(debug) System.out.println("<"+line); else System.out.println(line); return line; } protected void writeMsg(String msg)throws Exception //向服务器写消息 { pw.println(msg); pw.flush(); System.out.println(">"+msg); //控制命令用>开头 } protected void closeConnection() throws Exception //关闭所有的writers,sockets,随treams { pw.flush(); pw.close(); br.close(); socket.close(); } protected void sendQuit() throws Exception //发送关闭命令 { System.out.println("sending QUIT"); writeMsg("QUIT"); readline(true); System.out.println("closing connection"); closeConnection(); } protected void displayEmails() throws Exception //显示邮件相关信息 { BufferedReader userinput=new BufferedReader(new InputStreamReader(System.in)); System.out.println("dispalying mailbox with protocol commadns and responses below"); System.out.println("--------------------------------------"); System.out.println("opening socket"); socket=new Socket(this.hostname,this.port); br=new BufferedReader(new InputStreamReader(socket.getInputStream())); //socket的输入输出流 pw=new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); if(!responseIsOK()) //测试连接是否正常 { socket.close(); throw new Exception("invalid pop3 server"); } System.out.println("sending username"); writeMsg("USER "+this.username); //写入用户名 if(!responseIsOK()) //测试用户名是否正确 { sendQuit(); throw new Exception("invalid username"); } System.out.println("sending password"); writeMsg("PASS "+this.password); //写入密码 if(!responseIsOK()) //测试密码是否正确 { sendQuit(); throw new Exception("invalid password"); } System.out.println("checking mail"); //从server处得到邮件数量 writeMsg("STAT"); String line=readline(true); StringTokenizer tokens=new StringTokenizer(line," "); tokens.nextToken(); int messages=Integer.parseInt(tokens.nextToken()); int maxsize=Integer.parseInt(tokens.nextToken()); System.out.println(messages); System.out.println(maxsize); if(messages==0) { System.out.println("there are no messages"); sendQuit(); return; } System.out.println("there are"+messages+"messages"); System.out.print("press enter to continue"); userinput.readLine(); for(int i=1;i<=messages;i++) { System.out.println("retriving message number"+i); writeMsg("RETR "+i); System.out.println("------------------"); line=readline(false); while(line!=null&&!line.equals(".")) { line=readline(false); } System.out.println("-------------------"); System.out.println("press enter to continue."+"to stop,type Q then enter"); String response=userinput.readLine(); if(response.toUpperCase().startsWith("Q")) break; } sendQuit(); } public static void main(String[] args) throws Exception{ pop3 client=new pop3(); } protected void getInput()throws Exception { String data=null; BufferedReader br=new BufferedReader(new InputStreamReader(System.in) ); System.out.println("please enter pop3 server hostname"); data=br.readLine(); if(data==null||data.equals(""))hostname="localhost"; else hostname=data; System.out.println("please enter mailbox username:"); data=br.readLine(); if(!(data==null||data.equals(""))) username=data; System.out.println("please enter mailbox password:"); data=br.readLine(); if(!(data==null||data.equals(""))) password=data; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -