Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Cannot connect to pop3 or imap Store. Please help.

843834Mar 6 2009 — edited Mar 6 2009
Hi all,
I am building a email client.
I have a problem with connecting to the POP3 and/or IMAP server.
private static String host;
private static String username;
private static String password;
private static int port;

    public static void main(String[] args) throws Exception {
        host = "pop.gmail.com";
        port = 995;
        username = "xxxxxxxx@gmail.com";
        password = "xxxxxxxx";
        Properties props = new Properties();
        props.setProperty("mail.pop3.host", host);
        props.setProperty("mail.pop3.port", "" + port);
        props.setProperty("mail.pop3.user", username);
        props.setProperty("mail.pop3.timeout", "60000");
        Session session = Session.getDefaultInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication(username, password);
            }
        });
        Store store = session.getStore("pop3");
        store.connect();

        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        Message[] messages = folder.getMessages();
        for (int i = 0; i < messages.length; i++) {
            System.out.println(
                    i + " : " +
                    messages.getFrom()[0] + "\t" +
messages[i].getSubject() + "\t" +
messages[i].getSentDate() + "\n\n");
messages[i].writeTo(System.out);
}
folder.close(false);
store.close();
}
On the GMAIL account I am trying to connect to I have enabled both  [POP and IMAP|http://mail.google.com/support/bin/answer.py?answer=13273] , and have followed all instructions as indicated here:
[POP|http://mail.google.com/support/bin/answer.py?answer=13287] 
[IMAP|http://mail.google.com/support/bin/answer.py?answer=78799]

Any help would be greatly appreciated.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 3 2009
Added on Mar 6 2009
5 comments
162 views