我开始在python中使用一个非常简单的电子邮件程序,我试图从我的GMail地址发送一条消息。我总是得到以下错误,
Traceback (most recent call last): File "C:\Users\BarmanJa\Documents\PythonfromShell\TT\infi.py", line 15, in <module> server = smtplib.SMTP('smtp.gmail.com', 587) File "C:\Users\BarmanJa\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 251, in __init__ (code, msg) = self.connect(host, port) File "C:\Users\BarmanJa\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 336, in connect self.sock = self._get_socket(host, port, self.timeout) File "C:\Users\BarmanJa\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 307, in _get_socket self.source_address) File "C:\Users\BarmanJa\AppData\Local\Programs\Python\Python36-32\lib\socket.py", line 724, in create_connection raise err File "C:\Users\BarmanJa\AppData\Local\Programs\Python\Python36-32\lib\socket.py", line 713, in create_connection sock.connect(sa) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
我已经尝试了所有可能的方法来纠正它。我解决了其他人在社区发布的所有类似问题,但没有任何变化。以下是原始代码
import smtplib
mailfrom = 'jayantamgr@gmail.com'
msg = 'Hello'
mailto = ('myname@yahoo.com')
username = 'hisname@gmail.com'
password = 'wwwwwwwwww'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
s.starttls()
server.login(username, password)
server.sendmail(mailfrom, mailto , msg)
server.quit()
答案 0 :(得分:0)
错误消息表明您无法连接到目标计算机(smtp.gmail.com
):
ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝它
我怀疑您有某种阻止与SMTP提交端口连接的防火墙或防病毒软件,因为指定的主机名(smtp.gmail.com
)和端口号都是正确的(587)。
您的代码中也存在(无关)错误:s.starttls()
应为server.starttls()
。