本文介紹了Java郵件無法使用TLS或SSL連接到SMTP的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
0
我正在嘗試從Java連接到郵件服務器。我已經能夠使用相同的代碼成功地從Java連接到許多郵件服務器,包括Gmail、Rackspace、GoPardy和其他,但無論我嘗試什么設置,這個都不起作用。
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", this.outgoingHost);
props.put("mail.smtp.port", 587);
props.put("mail.smtp.ssl.trust", this.outgoingHost);
session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
出現以下錯誤:javax.mail.MessagingException:無法將套接字轉換為TLS;嵌套異常為:javax.net.ssl.SSLException:無法識別的SSL消息,是否為明文連接?
我也試過了
props.put("mail.smtp.host", this.outgoingHost);
props.put("mail.smtp.port", 587);
props.put("mail.smtp.socketFactory.port", 587);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.trust", this.outgoingHost);
session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
失敗,錯誤為:javax.mail.SendFailedException:地址無效;嵌套異常為:com.sun.mail.smtp.SMTPAddressFailedException:端口587上的郵件提交需要550 SMTP身份驗證
我也嘗試了端口465,但超時。
我能夠使用相同的SMTP設置從雷鳥電子郵件客戶端(相同的主機/端口/用戶/PW,但它必須執行其他操作)發送郵件。
使用Java Mail 1.51
有什么想法嗎?
推薦答案
當您使用SSL時(smtps
),您不使用STARTTLS(msa
),反之亦然。SSL默認為端口465
,TLS默認為端口587
。您可能還必須使用mail.smtp.ssl.protocols
或mail.smtps.ssl.protocols
設置SSL協議,以指定將為SSL/TLS連接啟用的SSL協議。最好避免覆蓋PasswordAuthentication
發送憑據,使用SMTPTransport
connect
方法。同樣重要的是,對于SSL,您必須使用smtps
formail.transport.protocol
,并使用mail.smtps
道具而不是mail.smtp
。我將提供關于SSL和TLS的示例。
您可以使用session.setDebug(true)
或props.put("mail.debug", "true")
在控制臺中調試并查看整個通信;這將非常有幫助,因為您將看到與服務器的整個telnet通信。
使用TLS(STARTTLS)587
:
// Set debug so we see the whole communication with the server
props.put("mail.debug", "true");
props.put("mail.transport.protocol", "smtp");
props.put("mail.host", outgoingHost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
// Enable STARTTLS
props.put("mail.smtp.starttls.enable", "true");
// Accept only TLS 1.1 and 1.2
props.setProperty("mail.smtp.ssl.protocols", "TLSv1.1 TLSv1.2");
Session session = Session.getInstance(props, null);
session.setDebug(true);
// Create an SMTP transport from the session
SMTPTransport t = (SMTPTransport)session.getTransport("smtp");
// Connect to the server using credentials
t.connect(outgoingHost, username, password);
// Send the message
t.sendMessage(msg, msg.getAllRecipients());
使用STARTTLS:
的調試輸出示例
注意傳輸協議是smtp
DEBUG: JavaMail version 1.5.1
...
DEBUG: setDebug: JavaMail version 1.5.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.example.com", port 587, isSSL false
220-srv.example.com ESMTP Exim 4.92 #2 Wed, 18 Mar 2020 15:55:56 +0200
220-We do not authorize the use of this system to transport unsolicited,
220 and/or bulk e-mail.
DEBUG SMTP: connected to host "mail.example.com", port: 587
EHLO host.docker.internal
250-srv.example.com Hello ppp.home.provider.com [x.x.x.x]
250-8BITMIME
250-PIPELINING
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 ""
STARTTLS
220 TLS go ahead
EHLO host.docker.internal
250-srv.example.com Hello ppp.home.provider.com [x.x.x.x]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
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 "HELP", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
250 OK
RCPT TO:<[email protected]>
250 Accepted
DEBUG SMTP: Verified Addresses
DEBUG SMTP: [email protected]
DATA
354 Enter message, ending with "." on a line by itself
Date: Wed, 18 Mar 2020 15:55:57 +0200 (EET)
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Test from JAVA!
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: smtpsend
Message from the JAVA app STARTTLS!
.
250 OK id=1jEZAt-009Y7x-5Z
Response: 250 OK id=1jEZAt-009Y7x-5Z
QUIT
221 srv.example.com closing connection
使用SSL465
:
// Set debug so we see the whole communication with the server
props.put("mail.debug", "true");
// All mail props for protocol will be mail.smtps
// We set smtps transport protocol for SSL
props.put("mail.transport.protocol", "smtps");
props.put("mail.host", outgoingHost);
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.port", "465");
props.put("mail.smtps.ssl.trust", outgoingHost);
props.put("mail.smtps.ssl.enable", "true");
// Accept only TLS 1.1 and 1.2
props.setProperty("mail.smtps.ssl.protocols", "TLSv1.1 TLSv1.2");
Session session = Session.getInstance(props, null);
session.setDebug(true);
// Create an SMTP transport from the session
SMTPTransport t = (SMTPTransport)session.getTransport("smtps");
// Connect to the server using credentials
t.connect(outgoingHost, username, password);
// Send the message
t.sendMessage(msg, msg.getAllRecipients());
使用SSL的調試輸出示例:
請注意,傳輸協議是smtps
DEBUG: JavaMail version 1.5.1
...
DEBUG: setDebug: JavaMail version 1.5.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.example.com", port 465, isSSL true
220-srv.example.com ESMTP Exim 4.92 #2 Wed, 18 Mar 2020 16:09:51 +0200
220-We do not authorize the use of this system to transport unsolicited,
220 and/or bulk e-mail.
DEBUG SMTP: connected to host "mail.example.com", port: 465
EHLO host.docker.internal
250-srv.example.com Hello ppp.home.provider.com [x.x.x.x]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
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 "HELP", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
250 OK
RCPT TO:<[email protected]>
250 Accepted
DEBUG SMTP: Verified Addresses
DEBUG SMTP: [email protected]
DATA
354 Enter message, ending with "." on a line by itself
Date: Wed, 18 Mar 2020 16:09:50 +0200 (EET)
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Test from JAVA!
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: smtpsend
Message from the JAVA app SSL!
.
250 OK id=1jEZOK-009bbA-5C
Response: 250 OK id=1jEZOK-009bbA-5C
QUIT
221 srv.example.com closing connection
這篇關于Java郵件無法使用TLS或SSL連接到SMTP的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,