Java-программа не может подключиться к моей учетной записи Gmail - PullRequest
1 голос
/ 28 октября 2011

Я нашел некоторый код Java, который реализует некоторые методы pop3, и немного его изменил. Мой код успешно подключается к серверу pop3, но не может подключиться к учетной записи Google. Имя пользователя и пароль верны. Я получаю следующее исключение при входе в метод POP3Client (имя пользователя String, пароль String)

   Exception in thread "main" java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.security.ssl.InputRecord.readFully(Unknown Source)
    at sun.security.ssl.InputRecord.read(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
    at sun.security.ssl.AppInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at POP3Client.readResponseLine(POP3Client.java:56)
    at POP3Client.sendCommand(POP3Client.java:71)
    at POP3Client.login(POP3Client.java:75)
    at Main.main(Main.java:8)

А вот код:

public class POP3Client {
    private Socket clientSocket;
    private boolean debug = false;
    private BufferedReader in;
    private BufferedWriter out;
    private static final int PORT = 995;//110;

    public boolean isDebug() {
        return debug;
    }

    public void connect(String host, int port) throws IOException {
        debug = true;

        SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
        clientSocket = (SSLSocket) sslsocketfactory.createSocket(host, PORT);

        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
        if (debug)
            System.out.println("Connected to the host");
        readResponseLine();
    }

    public void connect(String host) throws IOException {
        connect(host, PORT);
    }

    public boolean isConnected() {
        return clientSocket != null && clientSocket.isConnected();
    }

    public void disconnect() throws IOException {
        if (!isConnected())
            throw new IllegalStateException("Not connected to a host");
        clientSocket.close();
        in = null;
        out = null;
        if (debug)
            System.out.println("Disconnected from the host");
    }

    protected String readResponseLine() throws IOException{
        String response = in.readLine();
        if (debug) {
            System.out.println("DEBUG [in] : " + response);
        }
        if (response.startsWith("-ERR"))
            throw new RuntimeException("Server has returned an error: " + response.replaceFirst("-ERR ", ""));
        return response;
    }

    protected String sendCommand(String command) throws IOException {
        if (debug) {
        System.out.println("DEBUG [out]: " + command);
        }
        out.write(command + "\n");
        out.flush();
        return readResponseLine();
    }

    public void login(String username, String password) throws IOException {
        sendCommand("USER " + username);
        sendCommand("PASS " + password);
    }

    public void logout() throws IOException {
        sendCommand("QUIT");
    }

    public int getNumberOfNewMessages() throws IOException {
        String response = sendCommand("STAT");
        String[] values = response.split(" ");
        return Integer.parseInt(values[1]); //value[0] - busena, value[1] - pranesimu skaicius value[2] - pranesimu uzimama vieta
    }
}   


class Main{
    public static void main(String[] args) throws IOException {
        POP3Client client = new POP3Client();
        client.connect("pop3.live.com");
        client.login("username@gmail.com", "pasw");
        System.out.println("Number of new emails: " + client.getNumberOfNewMessages());
        LinkedList<Message> messages = client.getMessages();
        for (int index = 0; index < messages.size(); index++) {
        System.out.println("--- Message num. " + index + " ---");
        System.out.println(messages.get(index).getBody());
        }
        client.logout();
        client.disconnect();
    }
}

Я сделал это. К сожалению, я получаю огромный выход с таким окончанием

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at POP3Client.getMessage(POP3Client.java:99)
    at POP3Client.getMessages(POP3Client.java:125)
    at Main.main(Main.java:10)
DEBUG [in] : Received: by 10.101.180.22 with SMTP id h22mr4612373anp.149.1310909060085;
DEBUG [in] :         Sun, 17 Jul 2011 06:24:20 -0700 (PDT)
DEBUG [in] : Return-Path: <noreply-b751e365@plus.google.com>
DEBUG [in] : Received: from mail-iw0-f200.google.com (mail-iw0-f200.google.com [209.85.214.200])
DEBUG [in] :         by mx.google.com with ESMTPS id y2si4984786icw.36.2011.07.17.06.24.19 

Я набрал правильный адрес электронной почты и пароль Google

1 Ответ

0 голосов
/ 28 октября 2011

использование


client.connect("pop.gmail.com");

...