如何修复 ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] 版本号错误 (_ssl.c:1056)?

问题描述 投票:0回答:5

我正在尝试使用 python 发送电子邮件,但它一直显示

ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)
。这是我的代码:

server = smtplib.SMTP_SSL('smtp.mail.com', 587)
server.login("[email protected]", "password")
server.sendmail(
"[email protected]", 
"[email protected]", 
"email text")
server.quit()

你知道出了什么问题吗?

python ssl smtplib
5个回答
87
投票

SSL
的端口是465而不是587,但是当我使用
SSL
时,邮件到达了垃圾邮件。

对我来说,有效的方法是使用

TLS
而不是常规
SMTP
而不是
SMTP_SSL

请注意,这是一种安全方法,因为

TLS
也是一种加密协议(如 SSL)。

import smtplib, ssl

port = 587  # For starttls
smtp_server = "smtp.gmail.com"
sender_email = "[email protected]"
receiver_email = "[email protected]"
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教程


3
投票

这就是我解决同样问题的方法

import ssl


sender = "[email protected]"
password = "password123"
    
where_to_email = "[email protected]"
theme = "this is subject"
message = "this is your message, say hi to reciever"
    
sender_password = password
session = smtplib.SMTP_SSL('smtp.yandex.ru', 465)
session.login(sender, sender_password)
msg = f'From: {sender}\r\nTo: {where_to_email}\r\nContent-Type: text/plain; charset="utf-8"\r\nSubject: {theme}\r\n\r\n'
msg += message
session.sendmail(sender, where_to_email, msg.encode('utf8'))
session.quit()

如果您想使用

yandex
邮件
you must
在设置中打开“原始代码”。


0
投票

通过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("[email protected]", password)
server.sendmail("[email protected]" , "[email protected]" , "I am trying out python email through coding")
server.quit()

#关闭不太安全的应用程序,以便在您的 Gmail 上使用此功能


0
投票

谷歌不再让你关闭此功能,这意味着无论你做什么它都无法工作,雅虎似乎也是如此


0
投票

当我在端口 587 上使用 SSL 时,我遇到了类似的错误,因为服务提供商没有在 SSL 上启用端口 465。如果我在端口 587 上使用 TLS,则会收到另一个错误 550,b'管理禁止 - 信封被阻止。然后我意识到在我的代码中我没有将默认电子邮件更改为正确的电子邮件。完成此操作后,我能够在端口 587 上并使用 TLS 发送电子邮件。

© www.soinside.com 2019 - 2024. All rights reserved.