[英]python 3 smtplib exception: 'SSL: WRONG_VERSION_NUMBER' logging in to outlook
The following code in python 3 raises an error on my computer, and I don't know how to log in properly: 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_')
The last line produces the following output:最后一行产生以下输出:
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)
The SMTP information (port, etc) I used is from a microsoft help site , other ports or domains for outlook I've tried result in the same error.我使用的 SMTP 信息(端口等)来自微软帮助站点,我尝试过的 Outlook 的其他端口或域导致了同样的错误。
The output of openssl version
is 1.0.1e 11 Feb 2013
openssl version
的输出是1.0.1e 11 Feb 2013
The answer of @user2884042 is almost right. @user2884042 的答案几乎是正确的。
According to https://docs.python.org/3/library/ssl.html :根据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.在 3.5 版更改:默认 ssl_version 从 PROTOCOL_SSLv3 更改为 PROTOCOL_TLS 以最大程度地与现代服务器兼容。
So, you need to replace 'PROTOCOL_SSLv3' by 'PROTOCOL_TLS', which leaves the code something like:因此,您需要将 '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')
Sometimes you are not even required to login.有时您甚至不需要登录。 Instead of the following line,而不是以下行,
$ connection.login('now_your_real_login_data@outlook.com', 'otherwise_SMTPServerDisconnect')
You can directly send an email using your credentials.您可以使用您的凭据直接发送电子邮件。
$ sender_email = "senderemail@example.com"
$ receiver_email = "receiveremail@example.com"
$ msg = "Hello from python!"
$ connection.sendmail(sender_email, receiver_email, msg)
To answer my own question: beginning with python 3.3, you don't have to hack the smtplib as in this answer , but instead you can pass a SSLContext
object when using starttls
.要回答我自己的问题:从 python 3.3 开始,您不必像本答案中那样破解 smtplib,而是可以在使用starttls
时传递SSLContext
对象。
However, be aware: if the login data is wrong, it will still raise an error.但是,请注意:如果登录数据错误,它仍然会引发错误。 If the login data is right, everything works fine if using the following code:如果登录数据正确,则使用以下代码一切正常:
import smtplib
import ssl
context = ssl.SSLContext(ssl.PROTOCOL_SSLv3)
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.