簡體   English   中英

如何修復 ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] 錯誤的版本號 (_ssl.c:1056)?

[英]How to fix ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)?

我正在嘗試用 python 發送電子郵件,但它一直說ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056) 這是我的代碼:

server = smtplib.SMTP_SSL('smtp.mail.com', 587)
server.login("something0@mail.com", "password")
server.sendmail(
"something0@mail.com", 
"something@mail.com", 
"email text")
server.quit()

你知道有什么問題嗎?

SSL的端口是 465 而不是 587,但是當我使用SSL ,郵件到達了垃圾郵件。

對我來說,有效的是在常規SMTP上使用TLS而不是SMTP_SSL

請注意,這是一種安全方法,因為TLS也是一種加密協議(與 SSL 不同)。

import smtplib, ssl

port = 587  # For starttls
smtp_server = "smtp.gmail.com"
sender_email = "my@gmail.com"
receiver_email = "your@gmail.com"
password = input("Type your password and press enter:")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
    server.ehlo()  # Can be omitted
    server.starttls(context=context)
    server.ehlo()  # Can be omitted
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)

感謝真正的python教程提供

通過python發送電子郵件的代碼:

import smtplib , ssl
import getpass
server = smtplib.SMTP_SSL("smtp.gmail.com",465)
server.ehlo()
server.starttls
password = getpass.getpass()   # to hide your password while typing (feels cool)
server.login("example@gmail.com", password)
server.sendmail("example@gmail.com" , "sender-example@gmail.com" , "I am trying out python email through coding")
server.quit()

#關閉不太安全的應用程序以使其在您的 Gmail 上工作

當客戶端不理解應該來自服務器的ServerHello消息時,通常會發生這種情況。 典型用例:

  1. 服務器使用客戶端不理解的協議版本(太新或太舊)進行響應
  2. 服務器響應完整的垃圾數據(這表明它未使用加密)

我對openssl的參數玩了一點(我沒有將所有嘗試都粘貼到下面的代碼段中),並且遇到了各種錯誤。 顯然,它是#2。 smtp.mail.com不使用加密

 [cfati@cfati-ubtu16x64-0:~]> ~/sopr.sh *** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages *** [064bit-prompt]> python3 -c "import ssl, smtplib;print(ssl.OPENSSL_VERSION);smtplib.SMTP_SSL(\\"smtp.mail.com\\", 587)" 2>&1 | grep rror ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:645) [064bit-prompt]> [064bit-prompt]> openssl s_client -connect smtp.mail.com:587 CONNECTED(00000003) 140043409938072:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:s23_clnt.c:794: --- no peer certificate available --- No client certificate CA names sent --- SSL handshake has read 7 bytes and written 305 bytes --- New, (NONE), Cipher is (NONE) Secure Renegotiation IS NOT supported Compression: NONE Expansion: NONE No ALPN negotiated SSL-Session: Protocol : TLSv1.2 Cipher : 0000 Session-ID: Session-ID-ctx: Master-Key: Key-Arg : None PSK identity: None PSK identity hint: None SRP username: None Start Time: 1567195256 Timeout : 300 (sec) Verify return code: 0 (ok) --- [064bit-prompt]> [064bit-prompt]> python3 -c "import smtplib;smtplib.SMTP(\\"smtp.mail.com\\", 587);print(\\"Done.\\")" Done. 

消除錯誤的方法:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM