can't access gmail
843830Sep 7 2007 — edited Feb 24 2008Hi all,
I am trying to make an email client that connects to various pop3 providers. At the moment I have my code working for all non-SSL enabled pop3 providers e.g. bluebottle, inbox.
The one I really want to get working though is gmail. I've read previous posts on here and I know that gmail requires an SSL connection and it uses port 995 instead of 110. However when I try to connect I get the following error
javax.mail.MessagingException: Connect failed;
nested exception is:
java.net.SocketException: Unconnected sockets not implemented
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:148)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
The code always breaks down at the store.connect(host,username,password) line.
Here is some of the code. Please have a look and see where I'm going wrong. Thanks a million
import javax.mail.*;
import javax.mail.internet.MimeMultipart;
import javax.net.ssl.SSLSocketFactory;
import java.util.*;
import java.io.*;
public class GetGoogleMail
{
static int maxsize = 50000;
int numToRetrieve = 0;
private Properties props;
private String host,username,password;
public void GetGoogleArray()
{
try{
Session session = Session.getInstance(props, null);
//Get the store
Store store = session.getStore("pop3");
session.setDebug(true);
System.out.println("Attempting connection.");
try{
store.connect(host, username, password);
System.out.println("Connected to Gmail!");
}catch(Exception exc)
{exc.printStackTrace();
}
//Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
final int msgCount = folder.getMessageCount();
System.out.println("Folder contains " + msgCount + " messages.");
folder.close(false);
store.close();
}
catch(Exception e){}
}
public GetGoogleMail(int numMesgs, String user, String pass, int maxSize)
{
maxsize = maxSize*1000;
try
{
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
props = System.getProperties();
props.setProperty( "mail.imap.socketFactory.class", SSL_FACTORY);
props.setProperty( "mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty( "mail.imap.socketFactory.fallback", "false");
props.setProperty( "mail.pop3.socketFactory.fallback", "false");
props.setProperty( "mail.imap.port", "993");
props.setProperty( "mail.imap.socketFactory.port", "993");
props.setProperty( "mail.pop3.port", "995");
props.setProperty( "mail.pop3.socketFactory.port", "995");
host = "pop.gmail.com";
username = user;
password = pass;
numToRetrieve = numMesgs;
}
catch (Exception ex)
{
}
}
public static void main(String args[])
{
GetGoogleMail gmail = new GetGoogleMail(2,"","",50000);
gmail.GetGoogleArray();
System.exit(0);
}
}