我正在编写一个代码,以使用Python和本地服务器(在我工作的地方)自动发送电子邮件。我不知道为什么会发生此错误。
我尝试使用模块smtplib-> smtplib.SMTP_SSL(热,端口)和smtplib.SMTP(热,端口)中的命令连接服务器,但是两者都不起作用。
import smtplib
server = smtplib.SMTP('IPfromCompanyServer')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\socket.py", line 727, in create_connection
raise err
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\socket.py", line 716, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
答案 0 :(得分:0)
确保正确使用邮件服务器的IP和端口号。而且某些客户(例如Gmail)不允许您在不禁用安全邮件传输功能的情况下自动发送邮件。
另外,您可以在登录之前将这段代码添加到程序中。
try:
self.smtp.ehlo()
self.smtp.starttls()
self.smtp.ehlo
except:
print "No TLS "
#login here
也许此链接可能会有所帮助: https://superuser.com/questions/1292420/sending-an-email-from-python-using-local-python-smtp-server