python 3 smtplib 异常:“SSL:WRONG_VERSION_NUMBER"登录到 Outlook [英] python 3 smtplib exception: 'SSL: WRONG_VERSION_NUMBER' logging in to outlook

查看:288
本文介绍了python 3 smtplib 异常:“SSL:WRONG_VERSION_NUMBER"登录到 Outlook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

导入smtplibconnection = smtplib.SMTP('smtp-mail.outlook.com', 587)连接.elo()connection.starttls()连接.elo()connection.login('_these_dont_matter@outlook.com', '_the_error_persists_')

最后一行产生以下输出:

回溯(最近一次调用最后一次):文件/usr/lib/python3.3/smtplib.py",第 366 行,在 getreply 中line = self.file.readline()文件/usr/lib/python3.3/socket.py",第 297 行,在 readinto返回 self._sock.recv_into(b)文件/usr/lib/python3.3/ssl.py",第 460 行,在 recv_into返回 self.read(nbytes, 缓冲区)文件/usr/lib/python3.3/ssl.py",第 334 行,已读v = self._sslobj.read(len, buffer)ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] 错误的版本号 (_ssl.c:1504)在处理上述异常的过程中,又发生了一个异常:回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件/usr/lib/python3.3/smtplib.py",第621行,登录AUTH_PLAIN + " " + encode_plain(用户,密码))文件/usr/lib/python3.3/smtplib.py",第 398 行,在 docmd 中返回 self.getreply()文件/usr/lib/python3.3/smtplib.py",第 370 行,在 getreply 中+ str(e))smtplib.SMTPServerDisconnected: 连接意外关闭: [SSL: WRONG_VERSION_NUMBER] 版本号错误 (_ssl.c:1504)

我使用的 SMTP 信息(端口等)来自 微软帮助站点、我尝试过的 Outlook 的其他端口或域都导致了同样的错误.

openssl version 的输出是 1.0.1e 11 Feb 2013

解决方案

@user2884042 的回答几乎正确.

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

<块引用>

在 3.5 版更改:默认 ssl_version 从 PROTOCOL_SSLv3 更改为 PROTOCOL_TLS,以最大程度地与现代服务器兼容.

因此,您需要将 'PROTOCOL_SSLv3' 替换为 'PROTOCOL_TLS',代码如下:

<预><代码>导入 smtplib导入 ssl上下文 = ssl.SSLContext(ssl.PROTOCOL_TLS)connection = smtplib.SMTP('smtp-mail.outlook.com', 587)连接.elo()connection.starttls(上下文=上下文)连接.elo()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 = "你好来自蟒蛇!";$ connection.sendmail(sender_email,receiver_email,msg)

The following code in python 3 raises an error on my computer, and I don't know how to log in properly:

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.

The output of openssl version is 1.0.1e 11 Feb 2013

解决方案

The answer of @user2884042 is almost right.

According to 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.

So, you need to replace 'PROTOCOL_SSLv3' by 'PROTOCOL_TLS', which leaves the code something like:


    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)

这篇关于python 3 smtplib 异常:“SSL:WRONG_VERSION_NUMBER"登录到 Outlook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆