我正在尝试使用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()
你知道怎么了吗?
答案 0 :(得分:16)
SSL
的端口是465,而不是587,但是当我使用SSL
时,邮件到达了垃圾邮件。
对我来说,有效的方法是在常规TLS
上使用SMTP
而不是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 tutorial
答案 1 :(得分:0)
通常在客户端不理解应该来自服务器的 ServerHello 消息时发生。典型用例:
我对 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.
摆脱错误的方法: