简体   繁体   English

如何使用Java Mail API验证SMTP服务器的SSL / TLS证书?

[英]How to validate SMTP server's SSL/TLS certificate using java mail API?

We are adding a functionality to send email through SMTP. 我们添加了通过SMTP发送电子邮件的功能。 Basically, when the user tries to add and save the SMTP server details through UI, I will need to validate the server's certificate. 基本上,当用户尝试通过UI添加和保存SMTP服务器详细信息时,我将需要验证服务器的证书。 I could get the "sending mail" code done; 我可以完成“发送邮件”代码; there are enough number of examples :) However, I'm trying to see if there is any way to get the certificate details from the SMTP server and validate at client side using java mail API; 有足够多的示例:)但是,我试图查看是否有任何方法可以从SMTP服务器获取证书详细信息并使用Java Mail API在客户端进行验证; I can see the Transport.connect() method implicitly validates the server certificate,but i'm trying to understand if there is any explicit way of doing it. 我可以看到Transport.connect()方法隐式地验证了服务器证书,但是我试图了解是否有任何明确的方法来进行验证。 In short, these are the 2 things i'm trying to achieve: 简而言之,以下是我要实现的两件事:

1) Get the SLL/TLS certificate from the server and prompt the user with relevant details(like, whether the certificate is valid). 1)从服务器获取SLL / TLS证书,并提示用户相关详细信息(例如,该证书是否有效)。 2) If the certificate is not valid, provide the option to add it to the key store. 2)如果证书无效,请提供将其添加到密钥存储区的选项。

Any hints to get this done through java mail API would be appreciated. 通过Java邮件API完成此操作的任何提示将不胜感激。

You can use openssl to get server certificates for SSL/TLS or STARTTLS. 您可以使用openssl获取SSL / TLS或STARTTLS的服务器证书。

For SSL/TLS: 对于SSL / TLS:

openssl s_client -connect smtp.gmail.com:465 -CApath /etc/ssl/ </dev/null

For STARTTLS: 对于STARTTLS:

openssl s_client -starttls smtp -crlf -connect smtp.gmail.com:587 -CApath /etc/ssl/ </dev/null

I don't know how to do this using the Java Mail API, but perhaps you can use an openssl wrapper for Java to execute the calls equivalent to the those in the command lines above. 我不知道如何使用Java Mail API来执行此操作,但是也许可以使用Java的openssl包装器来执行与上述命令行中的调用等效的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM