【问题标题】:How to fix ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)?如何修复 ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] 错误的版本号 (_ssl.c:1056)?
【发布时间】:2022-04-22 18:02:35
【问题描述】:

我正在尝试使用 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()

你知道哪里错了吗?

【问题讨论】:

  • 您的 OSPython 版本和 OpenSSL 版本是什么?另外如果你打开命令解释器,import ssl,print(ssl.OPENSSL_VERSION) 输出什么?
  • @CristiFati Windows 10 家庭版,3.7,OpenSSL 1.1.0j 2018 年 11 月 20 日

标签: python ssl smtplib


【解决方案1】:

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)

感谢real python tutorial提供。

【讨论】:

  • “不像”?你的意思是“不一样”吗?
  • 不确定你说的是不是语法,但 SSL 也是一种加密协议。
  • @moshevi 我认为@RadioControlled 意味着您可以重写... (just like SSL) 之类的答案,以表明它们都是加密协议。
  • 我不是母语人士,但对我来说,“不不像”意味着“就像”。如果是这个意思,那就对了。但我认为您试图表明差异而不是共性。
【解决方案2】:

通过 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()

#turn off LESS SECURE APPS 以使这项工作在您的 gmail 上运行

【讨论】:

    猜你喜欢
    • 2020-10-27
    • 2021-05-05
    • 2018-03-23
    • 1970-01-01
    • 2019-05-10
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    相关资源
    最近更新 更多