Search code examples
javaemailselenium-webdrivergmail-imap

I am not able to read email content using javax mail imap because less secure apps is no longer available now as google declares on 30th May 2022


Error which i faced internal.qaauto.framework.exceptions.EmailDriverException: javax.mail.AuthenticationFailedException: [AUTHENTICATIONFAILED] Invalid credentials (Failure)

at internal.qaauto.framework.drivers.email.ImapsEmailDriver.connect(ImapsEmailDriver.java:55)
at certainwebapptests.CreateSubAccount.setUp(CreateSubAccount.java:53)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:178)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)

Caused by: javax.mail.AuthenticationFailedException: [AUTHENTICATIONFAILED] Invalid credentials (Failure) at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:661) at javax.mail.Service.connect(Service.java:295) at internal.qaauto.framework.drivers.email.ImapsEmailDriver.connect(ImapsEmailDriver.java:45) ... 24 more

Here is my code try { Properties connectionProperties = new Properties();

        // Set IMAPS as store protocol


        connectionProperties.put("mail.store.protocol", "imaps");
        connectionProperties.put("mail.imaps.ssl.checkserveridentity", "false");

        // Create a session with mail server
        session = Session.getDefaultInstance(connectionProperties);

        // Get the Store, which is JavaMail name for the entity that holds the mails.
        store = session.getStore("imaps");

        // Connect the recently created Store
        reporter.debug("Connecting to " + MAIL_HOSTNAME + ":" + IMAP_PORT + " using " + MAIL_USER + "/" + MAIL_PASSWORD);
        store.connect(MAIL_HOSTNAME, IMAP_PORT, MAIL_USER, MAIL_PASSWORD);

        // Check that connection was successful
        checkConnection();

        // Select Inbox folder
        selectFolder("Inbox");
    } catch (MessagingException e) {
        reporter.error("Unable to connect to mail server " + MAIL_HOSTNAME + " through port " + IMAP_PORT + ". Using "
                + MAIL_USER + "/" + MAIL_PASSWORD + ". Reason: " + e.getMessage());
        throw new RuntimeException(e);
    }

Solution

  • You have to Create & use App Passwords

    Go to your Google Account. Select Security. Under "Signing in to Google," select App Passwords. You may need to sign in. If you don’t have this option or 2-Step Verification is not set up for your account.

    At the bottom, choose Select app and choose the app you using and then Select device and choose the device you’re using and then Generate. Follow the instructions to enter the App Password. The App Password is the 16-character code in the yellow bar on your device. Clink on Done.

    If you are getting cert error try this :

    String certificatesTrustStorePath = "C:\\jdk-17.0.2.8-hotspot\\lib\\security\\security\\cacerts";
    System.setProperty("javax.net.ssl.trustStore", certificatesTrustStorePath);
    

    If you are getting TTL/SSL error try this:

    System.setProperty("mail.pop3s.ssl.protocols", "TLSv1.2");
    System.setProperty("mail.smtp.ssl.protocols", "TLSv1.2");