-1

I have tried different Java(API Based) Programs to read Gmail, as below with correct Email/Passwords, but I am getting below Exception always. Is java APIs no longer works to read Gmails ?

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 {
            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("pop3s");

            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 = "correctEmailId@gmail.com";// change accordingly
        String password = "CorrectPassword";// change accordingly

        check(host, mailStoreType, username, password);

    }

}

Error log:

javax.mail.AuthenticationFailedException: [AUTH] Username and password not accepted. at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:209) at javax.mail.Service.connect(Service.java:295) at javax.mail.Service.connect(Service.java:176) at zEmails.CheckingMails.check(CheckingMails.java:31) at zEmails.CheckingMails.main(CheckingMails.java:71)

user207421
  • 305,947
  • 44
  • 307
  • 483
Ram
  • 35
  • 2
  • 6
  • javax.mail.AuthenticationFailedException: [AUTH] Username and password not accepted. at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:209) at javax.mail.Service.connect(Service.java:295) at javax.mail.Service.connect(Service.java:176) at zEmails.CheckingMails.check(CheckingMails.java:31) at zEmails.CheckingMails.main(CheckingMails.java:71) – Ram Feb 18 '20 at 08:31
  • This is The Exception I am getting. plz help if JAVA API at all can work on GMAIL ? – Ram Feb 18 '20 at 08:31
  • 5
    Ther is an "edit" button to post the Exception in the question instead of in the comments – jhamon Feb 18 '20 at 08:33
  • 1
    related: https://stackoverflow.com/questions/16512592/login-credentials-not-working-with-gmail-smtp – jhamon Feb 18 '20 at 08:35
  • Does this answer your question? [Login credentials not working with Gmail SMTP](https://stackoverflow.com/questions/16512592/login-credentials-not-working-with-gmail-smtp) – Linda Lawton - DaImTo Feb 18 '20 at 08:52
  • There could be a problem in your task if you are using Gmail Server. You either disable [two step verification](https://support.google.com/accounts/answer/1064203?hl=en) on your gmail id if it is enabled or should allow [low secure app](https://accounts.google.com/ServiceLogin/signinchooser?service=accountsettings&passive=1209600&osid=1&continue=https%3A%2F%2Fmyaccount.google.com%2Flesssecureapps&followup=https%3A%2F%2Fmyaccount.google.com%2Flesssecureapps&emr=1&mrp=security&csig=AF-SEnaolaCSa3Edm4I7%3A1582017313&flowName=GlifWebSignIn&flowEntry=ServiceLogin) – Seshidhar G Feb 18 '20 at 09:13
  • 1
    You are using POP3. [tag:gmail-imap] therefore has nothing to do with it. The problem here is obviously the username or password, not the Java API. Be reasonable. – user207421 Feb 18 '20 at 10:10

1 Answers1

0

Thanks Friends and Seshidhar G

Allowing 'low secure app' for the emailID am am able to connect and read Email. Currently I am working on rest of the requirements.

Ram
  • 35
  • 2
  • 6