【发布时间】:2014-05-27 14:08:42
【问题描述】:
使用此代码使用 localhost ip 发送电子邮件时出现错误,有什么建议可以解决套接字错误吗?
def SendTMail(self):
# Import the email modules we'll need
# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
#try:
fp = open('testint.txt', 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
testfile = 'TESTING REPORT'
fp.close()
me = '124@hotmail.co.uk'
you = '123@live.com'
msg['Subject'] = 'The contents of %s' % testfile
msg['From'] = me
msg['To'] = you
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('192.168.1.3')
s.sendmail(me, [you], msg.as_string())
s.quit()
错误如下图:
File "x:\example.py", line 6, in SendTMail s = smtplib.SMTP('192.168.1.3')
File "x:\Python27\lib\smtplib.py", line 251, in init (code, msg) = self.connect(host, port)
File "x:\Python27\lib\smtplib.py", line 311, in connect self.sock = self._get_socket(host, port, self.timeout)
File "x:\Python27\lib\smtplib.py", line 286, in _get_socket return socket.create_connection((host, port), timeout)
File "x:\Python27\lib\socket.py", line 571, in create_connection raise err –
error: [Errno 10051] A socket operation was attempted to an unreachable network
【问题讨论】:
-
能否请您也发布错误?
-
您的计算机中有 SMTP 服务器吗?
-
错误:[Errno 10051] 尝试对无法访问的网络进行套接字操作,此错误
-
好吧,我没有 SMTP 服务器,我正在尝试从我的本地网络(家庭)发送它
-
但 smtplib.SMTP 连接到 现有 SMTP 服务器以发送电子邮件。您需要先安装(和配置)一个