我正在使用java郵件通過smtp發送電子郵件。下面給出的SMTP設置:沒有ssl的Java郵件-PKIX路徑構建失敗:
Properties props = new Properties();
Object put = props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.user", smtpUser);
props.put("mail.smtp.auth", true);
props.put("mail.debug", mailDebug);
props.put("mail.smtp.port", port);
該smtp憑證已通過telnetting我的smtpHost與上述細節驗證。但是,當我在java郵件中使用上述設置時,出現以下異常。
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
Nov 29, 2012 11:54:40 AM com.Test main
SEVERE: null
javax.mail.AuthenticationFailedException: 535 Incorrect authentication data
當我添加一行:
props.put("mail.smtp.starttls.enable", false);
它再次生成相同的身份驗證失敗的異常。
如果我設置mail.smtp.starttls.enable到真正,認證成功,但我得到以下異常:
220 TLS go ahead
Nov 28, 2012 5:32:36 PM com.Test main
SEVERE: null
javax.mail.MessagingException: Could not convert socket to TLS;
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.smtp.SMTPTransport.startTLS(SMTPTransport.java:1918)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:652)
at javax.mail.Service.connect(Service.java:317)
通過關於第二個例外各種論壇主題去後,我運行InstallCert程序來獲取服務器的自簽名證書。該InstallCert拋出以下異常:
Opening connection to mydomain.com.au:443...
Starting SSL handshake...
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:542)
at sun.security.ssl.InputRecord.read(InputRecord.java:374)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:850)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1190)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1217)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1201)
at InstallCert.main(InstallCert.java:100)
Could not obtain server certificate chain
所以,看起來像我的服務器沒有SSL,但STARTTLS啓用。使用STARTTLS將郵件發送到沒有ssl的服務器的正確參數是什麼?
我也嘗試之一。按照常見問題中所述使用InstallCert,並且我收到了異常「javax.net.ssl.SSLException:無法識別的SSL消息,明文連接?」。看起來我的服務器根本沒有使用SSL。 – janenz00
也用InstallCert異常編輯我的問題。 – janenz00
InstallCert的問題在於,只有在您可以開始SSL連接時纔有效。如果您的服務器僅支持轉換爲SSL(TLS)連接的純文本連接,則InstalCert將不起作用。您可能需要查看[MailSSLSocketFactory](http://javamail.kenai.com/nonav/javadocs/com/sun/mail/util/MailSSLSocketFactory.html)類,這會使您更輕鬆地處理自簽名否則將不能驗證的證書。 –