如何修复 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] 错误的版本号 (_ssl.c:1056)
.这是我的代码:
I am trying to send an email with python, but it keeps saying ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)
. Here is my code:
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()
你知道哪里出了问题吗?
Do you know what is wrong?
推荐答案
SSL
的端口是 465 而不是 587,但是当我使用 SSL
时,邮件到达垃圾邮件.
The port for SSL
is 465 and not 587, however when I used SSL
the mail arrived to the junk mail.
对我来说,有效的是使用 TLS
而不是 SMTP_SSL
而不是 SMTP_SSL
.
For me the thing that worked was to use TLS
over regular SMTP
instead of SMTP_SSL
.
请注意,这是一种安全方法,因为 TLS
也是一种加密协议(与 SSL 不同).
Note that this is a secure method as TLS
is also a cryptographic protocol (not unlike 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 教程
provided thanks to the real python tutorial
这篇关于如何修复 ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] 错误的版本号 (_ssl.c:1056)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!