无法发送电子邮件__Connection拒绝错误

时间:2018-03-16 18:27:13

标签: python email

import smtplib

sender = 'user@gmail.com'
receivers = ['user_2@gmail.com']

message = """From: User <user@gmail.com>
To: To user_2 <user_2@gmail.com>
Subject: message
this is a test megssage.
"""

try:
    smtpObj = smtplib.SMTP('localhost')
    smtpObj.sendmail(sender, receivers, message)         
    print ("Successfully sent email")
except smtplib.SMTPException:
    print ("Error: unable to send email")

当我尝试运行此程序时,它显示错误:

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

此外,我需要放入localhost

2 个答案:

答案 0 :(得分:0)

确保在Gmail中允许安全性较低的应用

答案 1 :(得分:0)

我认为在您的本地计算机上您自己的smtp服务器没有运行。而不是使用自己的smtp服务器(localhost)试试这个: -

smtpObj = smtplib.SMTP('smtp.gmail.com',587)