采用SSL进行发送邮件,
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");
在本地完全没问题,但是换台电脑或者发布到线上linux服务器就出现如下错误
javax.mail.MessagingException: Could not connect to SMTP host: smtp.exmail.qq.com, port: 465;
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at ............................此处省略
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:171)
at sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:98)
at sun.security.ssl.TransportContext.kickstart(TransportContext.java:220)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:428)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:354)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:211)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
... 123 more
产生这个问题的原因是java版本不一致引起的
本地的
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
linux上的
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)
解决:
方案一:换java版本,比较麻烦
方案二:其实产生这个问题的根本原因:是在jdk的安装路径的/jre/lib/security/java.security文件jdk.tls.disabledAlgorithms配置了 TLSv1,TLSv1.1 。把这几个删了,然后重启java项目。
(保险点也可以把SSLv3也删掉,我这里的情况删掉TLSv1,TLSv1.1就可以了)
jdk.tls.disabledAlgorithms=SSLv3, RC4, DES, MD5withRSA, DH keySize < 1024, \
EC keySize < 224, 3DES_EDE_CBC, anon, NULL
附:
Linux如何找到jdk的安装路径
[root@nginx-4-all sone_v32_test]# which java
/usr/bin/java
[root@nginx-4-all sone_v32_test]# ll /usr/bin/java
lrwxrwxrwx. 1 root root 22 7月 15 2021 /usr/bin/java -> /etc/alternatives/java
[root@nginx-4-all sone_v32_test]# ll /etc/alternatives/java
lrwxrwxrwx. 1 root root 73 7月 15 2021 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.el7_9.x86_64/jre/bin/java
[root@nginx-4-all sone_v32_test]# ll /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.el7_9.x86_64/jre/bin/java
-rwxr-xr-x. 1 root root 8984 4月 21 2021 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.el7_9.x86_64/jre/bin/java
最后的/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.el7_9.x86_64/jre/bin/java 就是java的安装路径