gpt4 book ai didi

python 3 smtplib 异常 : 'SSL: WRONG_VERSION_NUMBER' logging in to outlook

转载 作者:太空宇宙 更新时间:2023-11-04 07:01:51 25 4
gpt4 key购买 nike

python 3 中的以下代码在我的计算机上引发错误,我不知道如何正确登录:

import smtplib
connection = smtplib.SMTP('smtp-mail.outlook.com', 587)
connection.ehlo()
connection.starttls()
connection.ehlo()
connection.login('_these_dont_matter@outlook.com', '_the_error_persists_')

最后一行产生以下输出:

Traceback (most recent call last):
File "/usr/lib/python3.3/smtplib.py", line 366, in getreply
line = self.file.readline()
File "/usr/lib/python3.3/socket.py", line 297, in readinto
return self._sock.recv_into(b)
File "/usr/lib/python3.3/ssl.py", line 460, in recv_into
return self.read(nbytes, buffer)
File "/usr/lib/python3.3/ssl.py", line 334, in read
v = self._sslobj.read(len, buffer)
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1504)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.3/smtplib.py", line 621, in login
AUTH_PLAIN + " " + encode_plain(user, password))
File "/usr/lib/python3.3/smtplib.py", line 398, in docmd
return self.getreply()
File "/usr/lib/python3.3/smtplib.py", line 370, in getreply
+ str(e))
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1504)

我使用的SMTP信息(端口等)来自a microsoft help site ,我尝试过的 outlook 的其他端口或域也会导致相同的错误。

openssl version 的输出是 1.0.1e 11 Feb 2013

最佳答案

@user2884042 的回答几乎是正确的。

根据 https://docs.python.org/3/library/ssl.html :

Changed in version 3.5: The default ssl_version is changed from PROTOCOL_SSLv3 to PROTOCOL_TLS for maximum compatibility with modern servers.

因此,您需要将“PROTOCOL_SSLv3”替换为“PROTOCOL_TLS”,这样代码如下:


import smtplib
import ssl
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
connection = smtplib.SMTP('smtp-mail.outlook.com', 587)
connection.ehlo()
connection.starttls(context=context)
connection.ehlo()
connection.login('now_your_real_login_data@outlook.com', 'otherwise_SMTPServerDisconnect')

有时您甚至不需要登录。而不是以下行,

$ connection.login('now_your_real_login_data@outlook.com', 'otherwise_SMTPServerDisconnect')

您可以使用您的凭据直接发送电子邮件。

$ sender_email = "senderemail@example.com"
$ receiver_email = "receiveremail@example.com"
$ msg = "Hello from python!"
$ connection.sendmail(sender_email, receiver_email, msg)

关于python 3 smtplib 异常 : 'SSL: WRONG_VERSION_NUMBER' logging in to outlook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19390267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com