2

我通过 msft outlook.com 发送电子邮件的代码适用于 Windows,但不适用于我的 linux 机器。知道如何解决这个问题吗?

import smtplib
smtp = smtplib.SMTP('smtp.live.com', port=587)
smtp.starttls()
smtp.login(username, password)

SMTPServerDisconnected: Connection unexpectedly closed: 
[Errno 1] _ssl.c:1359: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number

编辑:更多细节:gentoo linux with python 2.7.3, openssl 0.9.8x and 1.0.1c

4

1 回答 1

2

I bet the problem is on the other side of the wire. Hello, Microsoft!

I've tried logging in a number of times, and you won't believe me, but some servers will let me in, while others won't. Try doing smtp.ehlo() in order to find the server's hostname (by the way, you must issue EHLO at the beginning of your session, and immediately after STARTTLS).

All their servers have names BLU0-SMTP<somenumber>phx.gbl. Believe me or not, but servers with two digits in their name are OK, but those with three digits are not.

In [52]: s = smtplib.SMTP('smtp.live.com', port=587)

In [53]: s.ehlo()
Out[53]:
(250,
 'BLU0-SMTP17.phx.gbl Hello [188.134.8.114]\nTURN\nSIZE 41943040\nETRN\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\n8bitmime\nBINARYMIME\nCHUNKING\nVRFY\nTLS\nSTARTTLS\nOK')

In [54]: s.starttls()
Out[54]: (220, '2.0.0 SMTP server ready')

In [55]: s.ehlo()
Out[55]:
(250,
 'BLU0-SMTP17.phx.gbl Hello [188.134.8.114]\nTURN\nSIZE 41943040\nETRN\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\n8bitmime\nBINARYMIME\nCHUNKING\nVRFY\nAUTH LOGIN PLAIN\nOK')

In [56]: s.login(login, password)
Out[56]: (235, '2.7.0 Authentication succeeded')
In [42]: s = smtplib.SMTP('smtp.live.com', port=587)

In [43]: s.ehlo()
Out[43]:
(250,
 'BLU0-SMTP116.phx.gbl Hello [188.134.8.114]\nTURN\nSIZE 41943040\nETRN\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\n8bitmime\nBINARYMIME\nCHUNKING\nVRFY\nTLS\nSTARTTLS\nOK')

In [44]: s.starttls()
Out[44]: (220, '2.0.0 SMTP server ready')

In [45]: s.ehlo()
Out[45]:
(250,
 'BLU0-SMTP116.phx.gbl Hello [188.134.8.114]\nTURN\nSIZE 41943040\nETRN\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\n8bitmime\nBINARYMIME\nCHUNKING\nVRFY\nAUTH LOGIN PLAIN\nOK')

In [46]: s.login(login, password)
---------------------------------------------------------------------------
SMTPServerDisconnected                    Traceback (most recent call last)

Update: Hm, seems that it is a known issue with 1.0.1c.

于 2013-06-09T18:29:49.060 回答