Java程序无法连接到我的Gmail帐户

时间:2011-10-28 10:44:57

标签: java sockets gmail client pop3

我发现了一些java代码,它实现了一些pop3方法并对其进行了一些修改。我的代码成功连接到pop3服务器但无法连接到谷歌帐户。用户名和密码是正确的。我在POP3Client的方法登录(String username,String password)

中得到以下异常
   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 

我输入了正确的谷歌电子邮件地址和密码

1 个答案:

答案 0 :(得分:0)

使用


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