首页 文章

javax.mail.MessagingException当我尝试通过IMAP连接到Gmail时抛出

提问于
浏览
0

尝试通过IMAP连接连接到Gmail, grab javax.mail.MessagingException

public static StringBuffer getEmailByBody(String username, String password, String containsMsg) throws Exception {
    Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", "imaps");
    props.setProperty("mail.imap.ssl.enable", "true");

    Session session = Session.getDefaultInstance(props, null);
    Store store = session.getStore("imaps");
    try {
        store.connect("imap.gmail.com", username , password);
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new Exception("Fail in connection" + ex.getMessage());
    }

    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);

    Message[] messages = null;
    boolean isMailFound = false;
    Message confirmationMAil = null;

    // Search for mail MAX 10 runs
    for (int i = 0; i < 10; i++) {
        messages = folder.search(new BodyTerm(containsMsg), folder.getMessages());
        for(Message message : messages){
            if (!message.isSet(Flags.Flag.SEEN)) {
                confirmationMAil = message;
                isMailFound = true;
                break;
            }
        }
        // wait for 3 seconds if message is not found
        if (!isMailFound) {
            Thread.sleep(3000);
        }
    }

    // Search latests for unread mail
    for(Message message : messages){
        if (!message.isSet(Flags.Flag.SEEN)) {
            confirmationMAil = message;
            isMailFound = true;
            break;
        }
    }

    // Test fails if no unread mail was found from God
    if (!isMailFound) {
        throw new Exception("Could not find email");

        // Read the content of mail and launch registration URL
    } else {
        String line;
        StringBuffer buffer = new StringBuffer();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                confirmationMAil.getInputStream()));
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        return buffer;
    }
}

Stack Trace:

javax.mail.MessagingException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到所请求目标的有效证书路径;嵌套异常是:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法在com.sun.mail中找到所请求目标的有效证书路径.imap.IMAPStore.protocolConnect(IMAPStore.java:670)位于javax.mail.Service.connect(Service.java:364)的javax.mail.Service.connect(Service.java:245)位于insynctive.utils.MailManager . getEmailByBody(MailManager.java:39)在insynctive.utils.MailManager.getVerificationCode(MailManager.java:106)在insynctive.pages.insynctive.TwoFAPage.sendViaPrimaryEmail(TwoFAPage.java:49)在insynctive.tests.PersonFileTest.loginWith2FAEmail(PersonFileTest的.java:148)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在java的 . 在o的lang.reflect.Method.invoke(Method.java:483) rg.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)在org.testng.internal.Invoker.invokeMethod(Invoker.java:659)在org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)在处org.testng.internal.TestMethodWorker.run org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1153)(TestMethodWorker.java:108 )org.testng.TestRunner.privateRun(TestRunner.java:771)org.testng.TestRunner.run(TestRunner.java:621)atg.testng.SuiteRunner.runTest(SuiteRunner.java:357)atg.testng .SuiteRunner.runSequentially(SuiteRunner.java:352)在org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)在org.testng.SuiteRunner.run(SuiteRunner.java:259)在org.testng.SuiteRunnerWorker.runSuite( SuiteRunnerWorker.java:52)org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)org.testng.TestNG.runSuitesSequentially(TestNG.java:1199)atg.testng.TestNG.runSuitesL ocly(TestNG.java:1124)org.testng.TestNG.run(TestNG.java:1032)位于java.lang.Thread.run(Thread.java)的insynctive.runnable.RunnableTest.run(RunnableTest.java:36) :745)引起:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法在sun.security中找到所请求目标的有效证书路径.sl.Alerts.getSSLException(Alerts.java:192)at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1917)at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:301)at sun . security.ssl.Handshaker.fatalSE(Handshaker.java:295)at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1369)at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:156)at sun .security.ssl.Handshaker.processLoop(Handshaker.java:925)at sun.security.ssl.Handshaker.process_record(Handshaker.java:860)at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocke) tImpl.java:1043)在sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)在sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)在sun.security.ssl.SSLSocketImpl.startHandshake (SSLSocketImpl.java:1355)com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)位于com.sun的com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:354) . com.sun.mail.iap.Protocol . (Protocol.java:116)的com.sun.mail.imap.protocol.IMAPProtocol上的mail.util.SocketFetcher.getSocket(SocketFetcher.java:237) . (IMAPProtocol.java: 115)在com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:685)com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:636)... 29更多引起:sun . security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法在sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)找到所请求目标的有效证书路径)在sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)at atsun.security.validator.Validator.validate(Validator.java:260)at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)

Main:

public static void main(String[] args) throws Exception {
        MailManager.getEmailByBody("asd@gmail.com", "password", "Hello");
}

3 回答

  • 0

    尝试添加此行代码 prop.put("mail.imap.ssl.trust", "*");

  • 0

    问题是Gmail阻止了所有外部访问,我解决了该问题,取消了帐户中的所有gmail安全性 .

  • 0

    在调用Session类之前尝试添加它:

    System.setProperty("javax.net.ssl.trustStore", path to your cacerts file in your Java folder, inside your hard disk);
            System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
            Properties prop = new Properties();
            prop.setProperty("mail.pop3.starttls.enable", "false");
            prop.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            prop.setProperty("mail.pop3.socketFactory.fallback", "false");
            prop.setProperty("mail.pop3.port", port);
            prop.setProperty("mail.pop3.socketFactory.port", port);
            prop.setProperty("mail.imap.ssl.trust", "*");
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            prop.put("mail.smtp.ssl.socketFactory", sf);
    

相关问题