How to fix ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] 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 时,邮件到达了垃圾邮件。

对我来说有用的是使用 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提供。

通过 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 上运行

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

import ssl


sender = "youremail@yandex.ru"
password = "password123"
    
where_to_email = "reciever@anymail.com"
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()