user2149122
user2149122

Reputation: 171

Unable to retrieve emails from Gmail using Java

I am trying to write a simple program in Java, in order to check my mailbox and get whatever email is in the inbox.

I have read various tutorials and watched a video in youtube and I found out that most people do something similar to this

So the I have taken the code found there:

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

public class CheckingMails {

   public static void check(String host, String storeType, String user,
      String password) 
   {
      try {
      //create properties field
      Properties properties = new Properties();
      properties.put("mail.pop3.host", host);
      properties.put("mail.pop3.port", "995");
      properties.put("mail.pop3.starttls.enable", "true");
      Session emailSession = Session.getDefaultInstance(properties);
      //create the POP3 store object and connect with the pop server
      Store store = emailSession.getStore("pop3");
      store.connect(host, user, password);
      //create the folder object and open it
      Folder emailFolder = store.getFolder("INBOX");
      emailFolder.open(Folder.READ_ONLY); 
      // retrieve the messages from the folder in an array and print it
      Message[] messages = emailFolder.getMessages();
      System.out.println("messages.length---" + messages.length);
      for (int i = 0, n = messages.length; i < n; i++) {
         Message message = messages[i];
         System.out.println("---------------------------------");
         System.out.println("Email Number " + (i + 1));
         System.out.println("Subject: " + message.getSubject());
         System.out.println("From: " + message.getFrom()[0]);
         System.out.println("Text: " + message.getContent().toString());
      }
      //close the store and folder objects
      emailFolder.close(false);
      store.close();
      } catch (NoSuchProviderException e) {
         e.printStackTrace();
      } catch (MessagingException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   public static void main(String[] args) {

      String host = "pop.gmail.com";// change accordingly
      String mailStoreType = "pop3";
      String username = "[email protected]";// change accordingly
      String password = "********";// change accordingly
      check(host, mailStoreType, username, password);
   } 
}

But I can't get my mails. Instead this error appears:

javax.mail.AuthenticationFailedException: EOF on socket
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:104)
at javax.mail.Service.connect(Service.java:233)
at javax.mail.Service.connect(Service.java:134)
at CheckingMails.check(CheckingMails.java:29)
at CheckingMails.main(CheckingMails.java:69)

I have enabled the POP and IMAP accesibility features on my email, as well as enabled less secure connections.

I downloaded the necessary .jar files from mail.jar and activation.jar

After looking around a bit, I found out that the issue probably is coming from Google's side, meaning that the authentication is going worng. The password and the name is correct (I have checked that multiple times). I have enabled both IMAP and POP connections and I have allowed less secure connections, but still I get an javax.mail.AuthenticationFailedException error.

Any ideas why is this happening?

Upvotes: 1

Views: 473

Answers (3)

user2149122
user2149122

Reputation: 171

For the people that will struggle in the future in the same way I did:

After applying the suggestion from Bill Shannon, I got another error,which I found out that it was coming due to the fact that I hadn't configure the ssl certificates properly. Since I have little to no clue about those, I was searching around the internet to find

Properties properties = new Properties();
  properties.put("mail.imap.host", host);
  properties.put("mail.imap.user", user);
  properties.setProperty("mail.imap.ssl.enable", "true");
  properties.put("mail.imap.ssl.trust", "*");

I changed the pop3 protocol to IMAP, because the later allows me to perform more tasks, but for the sole purpose of retrieving my emails both work in the same way.

The last line, in my understanding will accept any certificate, which probably is not the right way to go about it, but at the moment I don't know which one is the correct one.

Thank you all for your help.

****** It is important that the user, enables the POP3/IMAP connections in their accounts and also turn on the less secure applications, as it was stated previously. ******

Upvotes: 0

Bill Shannon
Bill Shannon

Reputation: 29971

You're connecting on the SSL port but not enabling SSL. Follow the Gmail instructions in the JavaMail FAQ. Get rid of the port setting and set mail.pop3.ssl.enable.

And make sure you understand the limitations of pop3 vs. imap.

Upvotes: 1

Ramesh
Ramesh

Reputation: 340

do below change to your Gmail account to access it from outside

  1. Login to Gmail.
  2. Access the URL as https://www.google.com/settings/security/lesssecureapps
  3. Select "Turn on"

Upvotes: 0

Related Questions