我正在使用以下方法使用SMTP从Python发送邮件。 我已经尝试了很多次来运行此代码,但这是正确的,但它总是向我显示出相同的错误,即有人可以帮助我。
import smtplib
TO = 'xxx@gmail.com'
SUBJECT = 'TEST MAIL'
TEXT = 'Here is a message from python.'
gms = 'xxxx@gmail.com'
gm = 'www'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()`enter code here`
server.starttls()
server.login(gms, gm)
BODY = '\r\n'.join(['To: %s' % TO,
'From: %s' % gms,
'Subject: %s' % SUBJECT,
'', TEXT])
try:
server.sendmail(gms, [TO], BODY)
print('email sent')
finally:
print('Error sending mail')
server.quit()
错误是:-
Traceback (most recent call last):
File "E:/projects/Intern/Mail/Maill.py", line 10, in <module>
server = smtplib.SMTP('smtp.gmail.com', 587)
File "C:\Users\KD\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\KD\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\KD\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\Users\KD\AppData\Local\Programs\Python\Python36\lib\socket.py", line 724, in create_connection
raise err
File "C:\Users\KD\AppData\Local\Programs\Python\Python36\lib\socket.py", line 713, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
答案 0 :(得分:2)
这很可能是您的防火墙或ISP的本地问题,可能阻止了传出SMTP连接。 smtp.gmail.com应该没有其他原因超时-它应该始终在线。即使凭据无效,通常也可以从gmail服务器获取智能错误代码。
检查防病毒软件或防火墙。或者尝试使用Web / HTTP API代替SMTP发送电子邮件。
答案 1 :(得分:0)
检查防火墙和网络设置,并确保打开端口587
!该错误表明您甚至无法通过smtp.gmail.com
打开连接。