2011-10-28 18 views
1

pop3メソッドのいくつかを実装しているJavaコードが見つかりました。私のコードはpop3サーバーに接続していますが、Googleアカウントに接続できません。ユーザー名とパスワードは正しいです。私はそれをしたJavaプログラムが自分のGmailアカウントに接続できません

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("[email protected]", "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(); 
    } 
} 

:私は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) 

そしてここでは、コードの一部になってしまいます。 Saddly私はそのようなものが

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: <[email protected]> 
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のメールアドレスとパスワードを入力

+3

'pop3.live.com'はないGmailのPOP3サーバーが、Hotmailの者です。 .. – m0skit0

+0

ホストとして 'pop.gmail.com'を使用します。 –

+0

durp ...正しいホストが助けになるかもしれません! – Andy

答えて

0

使用

 

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

関連する問題