I managed to login to my account on gmail. After that, the problems begin.
I'm asking the server for the content of email number 1 (or any other number, it doesn't really matter), and for some reason, after I read a few lines from the input from the server, the socket gets closed. It's like gmail is blocking me for some reason.
Here's the source code:
import java.io.*;
import javax.net.ssl.*;
import java.net.UnknownHostException;
public class EmailService {
String server = "pop.gmail.com";
int port = 995;
String username = "username@gmail.com", password = "password";
SSLSocket socket;
BufferedReader input;
PrintWriter output;
public static void main(String[] args) {
new EmailService();
}
public EmailService() {
try {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
socket = (SSLSocket)sslsocketfactory.createSocket(server, port);
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// After each println you MUST flush the buffer, or it won't work properly.
// The true argument makes an automatic flush after each println.
output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
connect();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void connect() throws IOException, InterruptedException {
String response = validateOneLine();
System.out.println(response);
// Username
output.println("USER " + username);
response = validateOneLine();
System.out.println(response);
// Password
output.println("PASS " + password);
response = validateOneLine();
System.out.println(response);
output.println("STAT");
response = validateOneLine();
System.out.println(response);
output.println("RETR 1");
while (true) {
response = validateOneLine();
System.out.println(response);
}
}
public String validateOneLine() throws IOException {
//if (socket.isClosed())
//return null;
String response = input.readLine();
if (response == null)
return null;
if (response.startsWith("+OK"))
return response;
else {
output.println("QUIT");
socket.close();
return response;
}
}
}
Just notice that the infinite loop means nothing. When you read multiple lines from the server using POP3, the input ends with a single line containing the single character - ".".
This character isn't being printed at all.
The function "validateOneLine" simply reads one line of the server.
Here's an example for an invalid console output (The socket is being closed during the reading from the server):
+OK Gpop ready for requests from 79.182.39.223 j28pf3087288faa.8
+OK send PASS
+OK Welcome.
+OK 250 1372900
+OK message follows
Delivered-To: baraklevy21@gmail.com
Received: by 10.216.21.194 with SMTP id r44cs356185wer;
Sun, 14 Mar 2010 08:39:31 -0700 (PDT)
Received: by 10.224.12.13 with SMTP id v13mr1329785qav.46.1268581170252;
Sun, 14 Mar 2010 08:39:30 -0700 (PDT)
Return-Path: <nobody@server145.go-mama-hosting.com>
Received: from server145.go-mama-hosting.com ([209.249.66.144])
by mx.google.com with ESMTP id 6si3674703qwd.14.2010.03.14.08.39.29;
Sun, 14 Mar 2010 08:39:29 -0700 (PDT)
Received-SPF: neutral (google.com: 209.249.66.144 is neither permitted nor denied by best guess record for domain of nobody@server145.go-mama-hosting.com) client-ip=209.249.66.144;
Authentication-Results: mx.google.com; spf=neutral (google.com: 209.249.66.144 is neither permitted nor denied by best guess record for domain of nobody@server145.go-mama-hosting.com) smtp.mail=nobody@server145.go-mama-hosting.com
Received: from nobody by server145.go-mama-hosting.com with local (Exim 4.69)
java.net.SocketException: Socket is closed
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkEOF(Unknown Source)
at com.sun.net.ssl.internal.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 EmailService.validateOneLine(EmailService.java:70)
at EmailService.connect(EmailService.java:61)
at EmailService.<init>(EmailService.java:30)
at EmailService.main(EmailService.java:17)
The funny thing is that it worked once, and the output was:
Received: by 10.216.3.13; Sat, 17 Oct 2009 01:15:17 -0700 (PDT) Date: Sat, 17 Oct 2009 01:15:17 -0700 Message-ID: <8df8eee90910170115m3fc0ac64j@mail.gmail.com> Subject: Access Gmail on your mobile phone From: Gmail Team <mail-noreply@google.com> To: barak levy <baraklevy21@gmail.com> Content-Type: multipart/alternative; boundary=0016364d1c57e2601b04761d1c56 --0016364d1c57e2601b04761d1c56 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable The days of needing your computer to get to your inbox are long gone. Use Gmail on your mobile phone and access your email from anywhere. * Access Gmail from your phone =BB<http://www.google.com/intl/en/mobile/mail/index.html#utm_source=3Dwel-e= ml&utm_medium=3Deml&utm_campaign=3Den> * - The Gmail Team --0016364d1c57e2601b04761d1c56 Content-Type: text/html; charset=ISO-8859-1 <html> <font face="Arial, Helvetica, sans-serif"> <p>The days of needing your computer to get to your inbox are long gone. Use Gmail on your mobile phone and access your email from anywhere.</p> <p><strong><a href="http://www.google.com/intl/en/mobile/mail/index.html#utm_source=wel-eml&utm_medium=eml&utm_campaign=en"> Access Gmail from your phone »</a></strong></p> <p>- The Gmail Team</p> </font> </html> --0016364d1c57e2601b04761d1c56--
A dot was printed here, but I didn't copy it. But since then - it has never worked again.
Make notice that each time I run the program, the output is different. The socket get closed in different intervals.
What exactly is the problem?
For more information about connecting to gmail:
Configuring other mail clients - Gmail Help
Thank you for your help! :)


Sign In
Create Account


Back to top









