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

标签 java sockets gmail client pop3

我找到了一些实现pop3方法的java代码并对其进行了一些修改。我的代码成功连接到 pop3 服务器,但无法连接到 google 帐户。用户名和密码正确。我在 POP3Client 的方法登录(字符串用户名,字符串密码)中遇到以下异常

   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 电子邮件地址和密码

最佳答案

使用


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

关于Java程序无法连接到我的gmail帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7928213/

相关文章:

c - 服务器 - 浏览器仅在服务器终止后显示站点

javascript - 从 GMail URL 获取唯一电子邮件标识符

android - 使用 OAuth 和 Android 的 AccountManager 提供的 Google 帐户 token 通过 IMAP 连接到 GMAIL

python - 无法使用 python 发送具有多个附件和多个收件人的邮件 [to,cc,bcc]

java - JBoss 5.1 : jar in server lib with dependency

java - 谷歌地图在 Android 中的选项卡 fragment 上显示灰色

java - 使用包含 CDATA 的 XML 配置 MOXy 的 @XmlPath

node.js - NodeJS-错误 : write EPIPE on SOCKS5 proxy

c++ - 在 OS X 上轮询和选择奇怪的行为

java - 错误消息 = 所需的 MultipartFile 参数 'file' 不存在