使用 smtplib 库发送电子邮件时出现超时错误
Getting timeout error while using smtplib library to send email
这是我的代码:
import smtplib
connection = smtplib.SMTP("smtp.gmail.com")
connection.starttls()
connection.login(user="mymail@gmail.com", password="mypassowrd")
connection.sendmail(from_addr="mymail@gmail.com", to_addrs="recievermail@gmail.com", msg="Hello")
connection.close()
所以我收到了这个错误:
Traceback (most recent call last):
File "E:0Days-Python_programs\Day32\Birthday Wisher (Day 32) start\main.py", line 3, in <module>
connection = smtplib.SMTP("smtp.gmail.com")
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 310, in _get_socket
return socket.create_connection((host, port), timeout,
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\socket.py", line 843, in create_connection
raise err
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\socket.py", line 831, 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
Process finished with exit code 1
我开启了不太安全的应用程序访问:
我也关闭了所有安全步骤:
而且我也关闭了防火墙保护。
但没有任何效果。
所以请有人帮助我。
您需要指定端口。在这种情况下,它是 587
for TSL
.
不知何故它起作用了,但我没有深厚的知识来解释为什么。
我遇到了同样的问题,所以有一个解决方案:
connection = smtplib.SMTP("smtp.gmail.com", 587)
这是我的代码:
import smtplib
connection = smtplib.SMTP("smtp.gmail.com")
connection.starttls()
connection.login(user="mymail@gmail.com", password="mypassowrd")
connection.sendmail(from_addr="mymail@gmail.com", to_addrs="recievermail@gmail.com", msg="Hello")
connection.close()
所以我收到了这个错误:
Traceback (most recent call last):
File "E:0Days-Python_programs\Day32\Birthday Wisher (Day 32) start\main.py", line 3, in <module>
connection = smtplib.SMTP("smtp.gmail.com")
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 310, in _get_socket
return socket.create_connection((host, port), timeout,
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\socket.py", line 843, in create_connection
raise err
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\socket.py", line 831, 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
Process finished with exit code 1
我开启了不太安全的应用程序访问:
我也关闭了所有安全步骤:
而且我也关闭了防火墙保护。
但没有任何效果。
所以请有人帮助我。
您需要指定端口。在这种情况下,它是 587
for TSL
.
不知何故它起作用了,但我没有深厚的知识来解释为什么。
我遇到了同样的问题,所以有一个解决方案:
connection = smtplib.SMTP("smtp.gmail.com", 587)