使用Java实现POP客户端编程

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class POPClient {
    private Socket socket;
    private Scanner socketReader;
    private PrintWriter socketWriter;

    public POPClient(String server, int port) throws IOException {
        try {
            socket = new Socket(server, port);
            socketReader = new Scanner(socket.getInputStream());
            socketWriter = new PrintWriter(socket.getOutputStream(), true);

        } catch (IOException e) {
            throw new IOException("the url or port of the server is wrong");
        }
    }

    //send  command to the server
    private void sendCommand(String command) {
        socketWriter.println(command);
    }

    //get answer from the server
    private String getAnswer() {
        String answer = socketReader.nextLine();
        return answer;
    }

    //login the mailbox
    private boolean login(String userName, String password) {
        sendCommand("USER " + userName);
        //if username is wrong,return false
        if (!getAnswer().startsWith("+OK")) {
            System.out.println("Wrong user name");
            return false;
        }
        //if the username is right then send the password
        sendCommand("PASS " + password);
        //if the password if wrong return false
        if (!getAnswer().startsWith("+OK")) {
            System.out.println("Wrong password");
            return false;
        }
        return true;
    }
    //the content of the mail finished with a "."
    private String getContent(){
        String content=socketReader.nextLine();
        String temp=socketReader.nextLine();
        while (!temp.equals(".")){
            content+=temp;
            temp=socketReader.nextLine();
        }
        return content;
    }

    public static void main(String[] args) {
        Scanner clientInput = new Scanner(System.in);
        System.out.println("welcome to the mailbox");
        System.out.println("Please input the URL of POP3 server");
        String server=clientInput.nextLine();
        //String server = "pop3.126.com";
        System.out.println("Please input the port of POP3 server");
        int port=clientInput.nextInt();
        //int port = 110;
        try {
            POPClient popClient = new POPClient(server, port);
            System.out.println(popClient.getAnswer());
            clientInput.nextLine();
            System.out.println("Please input the username");
            String userName=clientInput.nextLine();
            //String userName = "homework_1236@126.com";
            System.out.println("please input the password");
            String password=clientInput.nextLine();
            //String password = "homework123";
            if (popClient.login(userName, password)) {
                client:
                while (true) {
                    System.out.println("Choose your operation:");
                    System.out.println("1.Check the amount of mails and the size of the mailbox(STAT)");
                    System.out.println("2.Check the size of a mail(LIST [msg])");
                    System.out.println("3.Check the information of a mail(RETR msg)");
                    System.out.println("4.Delete a mail(DELE msg)");
                    System.out.println("5.Quit from the mailbox(QUIT)");
                    int command = clientInput.nextInt();
                    int num;
                    switch (command) {
                        case 1:
                            popClient.sendCommand("STAT");
                            System.out.println(popClient.getAnswer());
                            break;
                        case 2:
                            System.out.println("Please input the number of the mail you want to check:");
                            num = clientInput.nextInt();
                            popClient.sendCommand("LIST " + num);
                            System.out.println(popClient.getAnswer());
                            break;
                        case 3:
                            System.out.println("Please input the number of the mail you want to check:");
                            num = clientInput.nextInt();
                            popClient.sendCommand("RETR " + num);
                            String answer=popClient.getAnswer();
                            if(answer.startsWith("+OK")){
                                System.out.println(answer);
                                System.out.println(popClient.getContent());
                            }

                            break;
                        case 4:
                            System.out.println("Please input the number of the mail you want to delete:");
                            num = clientInput.nextInt();
                            popClient.sendCommand("DELE " + num);
                            System.out.println(popClient.getAnswer());
                            break;
                        case 5:
                            break client;

                    }

                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值