使用 JavaMail 发送邮件时出现 javax.net.ssl.SSLException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1157592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:21:06  来源:igfitidea点击:

javax.net.ssl.SSLException when sending mail using JavaMail

javajavamail

提问by Rajesh Kumar J

javax.mail.MessagingException: Exception reading response;
  nested exception is:
        javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1764)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1523)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:453)
        at javax.mail.Service.connect(Service.java:313)
        at javax.mail.Service.connect(Service.java:172)
        at javax.mail.Service.connect(Service.java:121)
        at javax.mail.Transport.send0(Transport.java:190)
        at javax.mail.Transport.send(Transport.java:120)
        at javaapplication5.SendMail.send(SendMail.java:77)
        at javaapplication5.SendMailTest.main(SendMailTest.java:17)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
        at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(InputRecord.java:523)
        at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:355)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:789)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1112)
        at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:744)
        at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)
        at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:106)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
        at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:84)
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1742)
        ... 9 more

Can anybody help me to send a mail using JavaMail API using proxy?

有人可以帮助我使用 JavaMail API 使用代理发送邮件吗?

回答by Thorbj?rn Ravn Andersen

You are trying to do an SSL connection to a non-SSL port. This will not work.

您正在尝试与非 SSL 端口建立 SSL 连接。这是行不通的。

If you want to send mail through gmail, see the FAQ: http://java.sun.com/products/javamail/FAQ.html#gmail

如果您想通过 gmail 发送邮件,请参阅常见问题解答:http: //java.sun.com/products/javamail/FAQ.html#gmail

回答by BungleFeet

I was getting the same exception when trying to send email via the Hotmail SMTP server at smtp.live.com. Here are the settings that worked for me in the end:

尝试通过 smtp.live.com 上的 Hotmail SMTP 服务器发送电子邮件时,我遇到了同样的异常。以下是最终对我有用的设置:

mail.smtp.starttls.enable=true 
mail.smtp.port=587

回答by Erich

If you don't want to use SSL, and you're using smtp instead of smtps try these settings

如果您不想使用 SSL,并且您使用的是 smtp 而不是 smtps,请尝试这些设置

mail.smtp.starttls.enable=false
mail.transport.protocol=smtp

回答by Sheepy

As Andersen answered, doing an SSL connection (mail.smtp.ssl.enable=true) to a non-SSL port will throw this error.

正如 Andersen 回答的那样,mail.smtp.ssl.enable=true对非 SSL 端口进行 SSL 连接 ( ) 将引发此错误。

This is commonly caused by connecting to the wrong port, as many popular mail services use port 587 instead of default smtps port 465. This applies to GMail, Hotmail/Live Mail, and Yahoo Mail.

这通常是由于连接到错误的端口造成的,因为许多流行的邮件服务使用端口 587 而不是默认的 smtps 端口 465。这适用于GMailHotmail/Live MailYahoo Mail

My problem, however, is that Java Mail insists on using SSL even when I set ssl to false.

然而,我的问题是,即使我将 ssl 设置为 false,Java Mail 也坚持使用 SSL。

After tracing the source code, the problem is I used Session.getDefaultInstance, copied from some example code. It only creates a session with the given properties on the first call; subsequence calls will return that old session, instead of a new session.

追查源代码后,问题是我用的Session.getDefaultInstance,从一些示例代码中复制过来的。它只在第一次调用时创建一个具有给定属性的会话;子序列调用将返回旧会话,而不是新会话。

Switching to Session.getInstancemake sure it use the properties I pass in, and solved my "SSLException: Unrecognized SSL message".

切换以Session.getInstance确保它使用我传入的属性,并解决了我的“SSLException:无法识别的 SSL 消息”。

回答by Jose1755

I was facing this problem when using Gmail.

我在使用 Gmail 时遇到了这个问题。

In order to use Gmail I had to turn ON "Allow less secure apps".

为了使用 Gmail,我必须打开“允许安全性较低的应用程序”

This Gmail setting can be found at https://www.google.com/settings/security/lesssecureappsafter login the gmail account.

登录 Gmail 帐户后,可以在https://www.google.com/settings/security/lesssecureapps上找到此 Gmail 设置。