2
votes

Good evening SO

I'm trying to make wonderful piece of code that can retrieve message over the POP protocol. I've so far followed the RFC1939 (POP Specification).

And actually it's working with my web host's POP3 server (which is not over SSL).But when accessing GMail all I get is empty responses :(

I assume it's the SSL part that's "breaking" it.

In my "open" function that creates the Socket I have this:

public void open() throws UnknownHostException, IOException 
{
    if(this.SSL)
    {
        SSLSocketFactory fac = (SSLSocketFactory) SSLSocketFactory.getDefault();
        this.s = fac.createSocket(this.in_host, this.in_port);
    }
    else
    {
        this.s = new Socket(this.in_host, this.in_port);
    }

    this.out = new PrintWriter(s.getOutputStream(), true);
    this.in = new BufferedReader(new InputStreamReader(s.getInputStream()));
}

where SSL is a bool indicating the connection should be over SSL, and s is the Socket. in_host and in_port is the host (pop.gmail.com) and port (995). out and in is the streams I write and read to/from.

One of the worst parts is that it doensn't throw any errors. Where I normally get the response, I just get an empty string.

Question is: How do I connect to a POP server over SSL/TLS, preferably the Gmail server?

2

2 Answers

3
votes

What about something like this:

public void connect() throws Exception {

    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

    Properties pop3Props = new Properties();

    pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
    pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
    pop3Props.setProperty("mail.pop3.port",  "995");
    pop3Props.setProperty("mail.pop3.socketFactory.port", "995");

    URLName url = new URLName("pop3", "pop.gmail.com", 995, "",
            username, password);

    session = Session.getInstance(pop3Props, null);
    store = new POP3SSLStore(session, url);
    store.connect();

}

See this article for more info.

0
votes

declare socket s as SSLSocket . Then it will work