登录Gmail imaps(不使用javamail)标记错误时遇到问题

时间:2015-07-08 11:31:53

标签: java login gmail

我正在尝试在java中创建一个与gimaps说话的类型状态客户端程序,但是我无法登录。当我输入我的详细信息时,它会发回* BAD无效标记。我认为这与我发送的标签有关。这是代码然后我会解释更多。

     public class CMain  {
        static final String CRLF = "\r\n";
        public static String safeRead(BufferedReader readerC) {
        String readline = "";
        try {
            readline = readerC.readLine();
        }
        catch(IOException e) {
            System.out.println("Input/Output error, unable to read");
            System.exit(+1);
        }
        return readline;
    }
    public static void main(String[] args) {
        CRole currentC = new CRole();
        BufferedReader readerC = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("connecting: ");

        String payload2 = currentC.receive_acceptedStringFromS();
        System.out.println("Received from S: " + payload2);
        //connected - now decide if you want to login
        System.out.print("Choose a label among [LOGIN, QUIT]: ");
        String label1 = safeRead(readerC).equals("LOGIN") ? "1" : "2";


        switch(currentC.send_Choice1LabelToS(label1).getEnum()){
            case Choice1.LOGIN:
            System.out.print("Send to S: ");
            //When i get here I enter username@gmail.com password
            String payload3 = safeRead(readerC);
            currentC.send_loginStringToS(payload3);
            String response = currentC.receive_Choice2LabelFromS();
            System.out.println("Server: " + response);

            String payload4 = currentC.receive_OKStringFromS();
            System.out.println("Received from S: " + payload4);
            System.out.print("Choose a label among [INBOX, QUIT]: ");
            String label2 = safeRead(readerC).equals("INBOX") ? "1" : "2";
            switch(currentC.send_Choice3LabelToS(label2).getEnum()){
                case Choice3.INBOX:
                System.out.print("Send to S: ");
                String payload5 = safeRead(readerC);
                currentC.send_inboxStringToS(payload5); 
                break;
                case Choice3.QUIT:
                System.out.print("Send to S: ");
                String payload6 = safeRead(readerC);
                currentC.send_quitStringToS(payload6);
                break;
                }


        break;
        case Choice1.QUIT:
        System.out.print("Send to S: ");
        String payload8 = safeRead(readerC);
        currentC.send_quitStringToS(payload8);
        break;
        }
    }
}

    public class CRole  {
       private BufferedReader socketSIn = null;
       private PrintWriter socketSOut = null;
       private Socket client;
       public CRole(){
           SSLSocketFactory sslsocketfactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
        try {
            client = (SSLSocket) sslsocketfactory.createSocket("imap.gmail.com", 993);
            System.out.println("Connect to Host");
        }
        catch(IOException e) {
            System.out.println("Unable to listen on ports");
            System.exit(+1);
        }
        try {
            socketSIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
            socketSOut = new PrintWriter(client.getOutputStream(), true);
        }
        catch(IOException e) {
            System.out.println("Read failed");
            System.exit(+1);
        }
    }
    public void send_connectStringToS(String payload) {
        this.socketSOut.println(payload);
    }
    public String receive_acceptedStringFromS() {
        String line = "";
        try {
            line = this.socketSIn.readLine();
        }
        catch(IOException e) {
            System.out.println("Input/Outpur error.");
            System.exit(+1);
        }
        return line;
    }
    public Choice1 send_Choice1LabelToS(String payload) {
        this.socketSOut.println(payload);
        int intLabelChoice1 = Integer.parseInt(payload);
        //int intLabelChoice1 = 1;
        switch(intLabelChoice1){
            case 1:
            return new Choice1(Choice1.LOGIN);
            case 2:
            default:
            return new Choice1(Choice1.QUIT);
        }
    }
    public void send_loginStringToS(String payload) {
        this.socketSOut.println(payload);
    }
    public String receive_Choice2LabelFromS() {
        String line = "";
        try {
            line = this.socketSIn.readLine();
        }
        catch(IOException e) {
            System.out.println("Input/Outpur error.");
            System.exit(+1);
        }
        return line;
    }
    public String receive_OKStringFromS() {
        String line = "";
        try {
            line = this.socketSIn.readLine();
        }
        catch(IOException e) {
            System.out.println("Input/Outpur error.");
            System.exit(+1);
        }
        return line;
    }
    public Choice3 send_Choice3LabelToS(String payload) {
        this.socketSOut.println(payload);
        int intLabelChoice3 = Integer.parseInt(payload);
        switch(intLabelChoice3){
            case 1:
            return new Choice3(Choice3.INBOX);
            case 2:
            default:
            return new Choice3(Choice3.QUIT);
        }
    }
    public void send_inboxStringToS(String payload) {
        this.socketSOut.println(payload);
    }
    public void send_quitStringToS(String payload) {
        this.socketSOut.println(payload);
    }
    public String receive_quitStringFromS() {
        String line = "";
        try {
            line = this.socketSIn.readLine();
        }
        catch(IOException e) {
            System.out.println("Input/Outpur error.");
            System.exit(+1);
        }
        return line;
    }
}

这是我发送gmail的字符串的情况吗?我发送了以下字符串:username@gmail.com密码; a001登录username@gmail.com密码; a001 username@gmail.com密码; username@gmail.com,密码。我对这些没有好运。

或者它与OAuth有关吗?

我没有使用javamail,也不想使用它。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

问题半固定。我重写了一个简单的程序,不使用类型化编程方法和这项工作,所以它不发送错误的标签,但是类型化代码中有一些东西导致了问题。