Recently, I encountered a problem when doing java mail ssl: using JavaMail to receive emails cannot succeed after the system has been running for a certain period of time.
The error message is as follows:
javax.mail.MessagingException: Connect failed;
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.pop3.POP3Store.protocolConnect(POP3Store.java:148)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service. java:156)
at javax.mail.Service.connect(Service.java:105)
Or the mail server that does not enable SSL has the following error:
nested exception is:
java.net.ConnectException : Connection refused
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:148)
Telnet to the mail server, the username and password are OK. It seems that there is only a program problem.
Properties props = System.getProperties() was used when creating MailSession; when I printed this out, I found an error. SSL verification should not have been used, but the verification information should not have appeared, so now If you do not need an SSL link every time you connect to POP3, remove these attributes and the problem will be gone.
Finally I know who changed System.getProperties(); It turns out that there is another system running in the same Tomcat. The mail server used by that system uses SSL verification to send and receive emails, and will receive emails once every 30 minutes. Because the program was not written rigorously enough, the above errors occurred. After improving the program, the two systems could finally coexist in one TOMCAT.
The above is the detailed content of How to use both SSL and non-SSL connections in Java Mail?. For more information, please follow other related articles on the PHP Chinese website!