Why do i get this exception?

12,384

Solution 1

Try this :

Properties props = new Properties();
props.put("mail.pop3.host" , "pop.gmail.com");
props.put("mail.pop3.user" , "username");
// Start SSL connection
props.put("mail.pop3.socketFactory" , 995 );
props.put("mail.pop3.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
props.put("mail.pop3.port" , 995);

Session session = Session.getDefaultInstance(props , new Authenticator() {
    @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication( "username" , "password");
            }
});
try {
    Store store  = session.getStore("pop3");
    store.connect("pop.gmail.com" , "username" , "password");
    Folder fldr = store.getFolder("INBOX");
    fldr.open(Folder.HOLDS_MESSAGES);
    int count = fldr.getMessageCount();
    System.out.println(count);
} catch(Exception exc) {
    System.out.println(exc + " error");
}

Also visit this question

Solution 2

Probably because the server refuses to connect.

Try connecting from "telnet". Once you can connect at all, then you should be able to connect from your Java program.

Here are some troubleshooting tips:

Solution 3

Try changing

store.connect("pop.gmail.com" , "username" , "password");

to

store.connect("pop.gmail.com" , 995, "username" , "password");

Disclaimer: I have not tested this.

Gmail requires a secure SSL connection, and maybe javax.mail.Service isn't providing that. I think the more likely explanation, though, is that you're simply not connecting to the right port, so I've explicitly specified the correct port number for Gmail's POP3 service.

Solution 4

Try following a "how to use gmail as an smtp server" tutorial. Google also has a configuration page with all the settings you'll need.

Share:
12,384

Related videos on Youtube

saplingPro
Author by

saplingPro

I love to write code and love to be known as a programmer. I try very hard to be creative. I am a hard working man ! I don't know where i am heading and don't know where i will be :(

Updated on June 04, 2022

Comments

  • saplingPro
    saplingPro about 2 years

    This method gives the number of emails in the inbox.But it gives me this exception :

    javax.mail.MessagingException: Connect failed;
     nested exception is:
    java.net.ConnectException: Connection timed out: connecterror
    

    -

     Session session = Session.getInstance(new Properties());
        try {
            Store store  = session.getStore("pop3");
            store.connect("pop.gmail.com" , "username" , "password");
            Folder fldr = store.getFolder("INBOX");
            fldr.open(Folder.READ_WRITE);
            int count = fldr.getMessageCount();
            System.out.println(count);
        } catch(Exception exc) {
            System.out.println(exc + "error");
        }    
    
  • saplingPro
    saplingPro almost 13 years
    This is the exception reported then : javax.mail.AuthenticationFailedException: EOF on socketerror
  • Rax
    Rax about 8 years
    Hi i am getting javax.mail.AuthenticationFailedException: EOF on socket
  • Suhail Gupta
    Suhail Gupta about 8 years
    @RakshitSoni Suggests that you are using incorrect username and password. It could fail if you use double authentication on google. You will have to register your application with google(In case you use double authentication)