希望这不是一个不好的问题/已经回答过的问题(我想不是,所以我要问这个问题):
几个月前,我编写了一些Java代码,以使用JavaMail API连接到Gmail收件箱。它的工作没有任何问题,并且我使用它的次数越来越多。然后突然发生了一件非常奇怪的事情:几天前它停止工作,而没有对代码本身进行任何更改。
我在互联网上搜索了一种方法,可以找出试图更新代码而未成功的问题(我使用的是JavaMail API的最新版本)。
这是我的代码MailService.java的关键部分(就像我尝试对其进行更新/更正之前一样,并且直到几天前它都运行良好):
public static boolean checkMails (boolean lightOutput) throws MessagingException, IOException, ...others... {
Properties sessionProperties = new Properties();
sessionProperties.put("mail.smtp.host", "smtp.googlemail.com");
sessionProperties.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(sessionProperties);
Store store = null;
Folder inbox = null;
try {
if (!lightOutput) print(MessageType.INFO,"Connecting to account "+mail+"...\n");
store = session.getStore("imaps");
store.connect("imap.googlemail.com", 993, mail, password);
inbox = store.getFolder( "INBOX" );
inbox.open( Folder.READ_WRITE );
...etc etc...
这是引发的错误:
Exception in thread "main" com.sun.mail.util.MailConnectException:
Couldn't connect to host, port: imap.googlemail.com, 993; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:740)
at javax.mail.Service.connect(Service.java:366)
at operations.MailService.checkMails(MailService.java:134)
at operations.MailService.main(MailService.java:90)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:359)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:134)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:131)
at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:763)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:698)
... 3 more
在以下行中的MailService.java:134引发错误:
store.connect("imap.googlemail.com", 993, mail, password);
请注意, mail 和 password 是事先声明的(它们是正确的)。我还激活了我正在使用的gmail帐户的imap协议和less secure app access(因为这段时间代码正常工作),并且在代码停止工作之前和之后,我都没有对路由器和防火墙设置进行任何更改
有人可以帮我吗?
EDIT
我在与房屋的调制解调器相连的个人计算机上运行此代码(这是我在代码正常运行期间一直使用的配置)。我还尝试使用手机作为热点来更改互联网连接,但问题仍然相同。
我还将所有项目导出到一个可执行的.jar文件中,并在我的个人计算机上运行它,并且发生相同的问题(抛出相同的错误)。
但是我注意到了一些奇怪的事情:在另一台机器(通常我不使用的另一台计算机)上运行那个.jar可执行文件(将其连接到我的家用调制解调器)我遇到了一个完全不同的错误:
Exception in thread "main" javax.mail.MessagingException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:742)
at javax.mail.Service.connect(Service.java:366)
at operations.MailService.checkMails(MailService.java:134)
at operations.MailService.main(MailService.java:90)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
at sun.security.ssl.Handshaker.processLoop(Unknown Source)
at sun.security.ssl.Handshaker.process_record(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:626)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:400)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:134)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:131)
at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:763)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:698)
... 3 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
... 18 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
... 24 more
我真的不明白。