我的问题是,当我运行以下代码时,我得到一个WinError 10061,并且从我的所有搜索看起来这是因为外部机器没有正确设置,但我认为Google有这个照顾好gmail,所以错误就在我身边。我能找到的所有其他示例都使用localhost并收到此错误,这是因为他们没有设置本地邮件服务器。这个问题仍然存在吗?我相信我错过了一些明显的东西。此外,错误代码已完整。提前谢谢!
import smtplib
fromaddr = 'email@email.com'
toaddrs = 'otheremail@email.com'
msg = 'Random stuff!'
username = 'username'
password = 'pass'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
错误:
Traceback (most recent call last):
File "C:/Users/Brett/PycharmProjects/Texting/sendMessage.py", line 13, in <module>
server = smtplib.SMTP('smtp.gmail.com')
File "C:\Python33\lib\smtplib.py", line 238, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python33\lib\smtplib.py", line 317, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python33\lib\smtplib.py", line 288, in _get_socket
self.source_address)
File "C:\Python33\lib\socket.py", line 424, in create_connection
raise err
File "C:\Python33\lib\socket.py", line 415, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
答案 0 :(得分:0)
过去曾使用过Gmail SMTP服务器,我只能指出SMTP端口的数字实际默认为适用于Gmail的端口,因此端口参数(也可以添加到服务器名称上)冒号)可以省略。鉴于此,您可能应该尝试使用其他计算机以确保不会遇到网络错误等。此外,端口587要求进行ID检查,这意味着Gmail可能会认为您是垃圾邮件或其他一些废话。端口25是未输入端口号时的默认值,不执行此类检查。