#!/usr/bin/python
import os, sys
import smtplib
from email.mime.text import MIMEText
# GRABE LISTED DIRECTORY AND CREATE THE FILE #
#open folder and list it
Winpath = "C:\"
dirs = os.listdir(Winpath)
#Put it into file
fo = open("activitygraber.txt", "w+")
for file in dirs:
fo.write(file + "\n")
# EMAIL IT TO GIVEN ADDRESS #
msg = MIMEText(fo.read())
fo.close()
msg["Subject"] = "Test ActivityGrab"
msg["From"] = "test@test.me"
msg["To"] = "dest@dest.com"
s = smtplib.SMTP("localhost")
s.sendmail("test@test.me", "dest@dest.com", msg.as_string())
s.quit()
这个简单的代码主要来自Python文档(我在学习曲线中)。 正在创建文件并准备发送我猜,但不知何故连接没有被创建。我想这是因为我的smtp端口未打开?我应该尝试连接到邮件服务吗?然后从那里发送?
这是我尝试发送时收到的消息:
Traceback (most recent call last):
File "C:\Users\PC\Desktop\testfile.py", line 33, in <module>
s = smtplib.SMTP("localhost")
File "C:\Python27\lib\smtplib.py", line 250, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 310, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 285, in _get_socket
return socket.create_connection((host, port), timeout)
File "C:\Python27\lib\socket.py", line 571, in create_connection
raise err
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
任何提示的帮助都非常感谢! 与此同时,我将继续寻找原因。
干杯,
答案 0 :(得分:0)
s = smtplib.SMTP("localhost")
除非您在本地计算机上运行SMTP服务器(您可能不会......),否则无效。
如果你这样做,请确保它正在侦听端口25。
如果不这样做,则必须使用远程SMTP服务器。如果你有一个Gmail帐户,你可以使用google,或者找一个设置为邮件中继的免费帐户(这意味着你可以匿名使用它),但这些日子非常罕见。