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");
}
Try this :
Also visit this question
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:
http://www.anta.net/misc/telnet-troubleshooting/pop.shtml
https://www-304.ibm.com/support/docview.wss?uid=swg21097014
http://support.microsoft.com/kb/885685
Try changing
to
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.
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.