如何在 Java 中获取 SMTP 服务器的证书
how to get the certificate of an SMTP server in Java
最近我试图通过使用 Java 邮件和 Apache commons 电子邮件框架来验证 SMTP 服务器的证书,但我没有得到任何线索来验证 SMTP 服务器的证书(ssl 或使用 tls)而不发送任何 message/mail。我猜它可能使用 SSLSocket 来执行此操作,但我只是得到一个 SSLException 说 'the message should be plain' ,而不是证书异常(我预料到了)。有什么想法吗?
已编辑:(使用 sslsocket.connect)
我真的通过使用以下代码
获得了证书异常
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory
.getDefault();
SSLSocket s = (SSLSocket) factory.createSocket("localhost", 9999); //9999 is the SMTP SSL port
//this really throws a exception.
s.startHandshake();
System.out.println("ok");
例外情况是:
Exception in thread "main" 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(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1904)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:279)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:273)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1446)
我认为这可以帮助我确定证书是否有效。
我假设你的意思是 "smtp",而不是 "snmp"。
如果您收到 "should be plain" 错误,这意味着您连接到纯文本端口而不是 SSL 端口。
如果您只是想验证证书是否有效,而不登录,您应该使用 SSLSocket 并查看是否可以连接而不会出现异常。
如果要验证证书是否有效,验证信息是否有效,可以使用Transport.connect.
最近我试图通过使用 Java 邮件和 Apache commons 电子邮件框架来验证 SMTP 服务器的证书,但我没有得到任何线索来验证 SMTP 服务器的证书(ssl 或使用 tls)而不发送任何 message/mail。我猜它可能使用 SSLSocket 来执行此操作,但我只是得到一个 SSLException 说 'the message should be plain' ,而不是证书异常(我预料到了)。有什么想法吗?
已编辑:(使用 sslsocket.connect)
我真的通过使用以下代码
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory
.getDefault();
SSLSocket s = (SSLSocket) factory.createSocket("localhost", 9999); //9999 is the SMTP SSL port
//this really throws a exception.
s.startHandshake();
System.out.println("ok");
例外情况是:
Exception in thread "main" 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(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1904)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:279)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:273)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1446)
我认为这可以帮助我确定证书是否有效。
我假设你的意思是 "smtp",而不是 "snmp"。
如果您收到 "should be plain" 错误,这意味着您连接到纯文本端口而不是 SSL 端口。
如果您只是想验证证书是否有效,而不登录,您应该使用 SSLSocket 并查看是否可以连接而不会出现异常。
如果要验证证书是否有效,验证信息是否有效,可以使用Transport.connect.