【问题标题】:Python Send reply to an gmail fails: A connection attempt failed becausePython 向 gmail 发送回复失败:连接尝试失败,因为
【发布时间】:2019-01-05 06:12:34
【问题描述】:

Python 向 gmail 帐户中的现有电子邮件发送回复失败并出现以下错误:

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

我的 gmail 帐户中有一封由自动化实体发送的电子邮件。使用我的 python 代码,我收到它的 UID 并尝试回复它,但它失败了。

我阅读了几篇关于 gmail 回复的 stackoverflow 帖子,甚至阅读了 https://www.rfc-editor.org/rfc/rfc2822#appendix-A.2Gmail Send Email As Reply

# An automation entity (abc@myNet.email.com) sends an email to my gmail Account xyz@gmail.com 
    
    from: abc@myNet.email.com  
    to: xyz@gmail.com        
    date: Jan 3, 2019, 7:19 AM 
    subject: abc Email Automation (Id=100) 
    security:  No encryption Learn more 

#Using IMAP4 I retrieve the UID of the last email with a given subject ("abc Email Automation"), 
    say --> 904

 # In python gmail reply 
    message = "I received your email"
    to_email = "abc@myNet.email.com "
    servername = 'smtp.gmail.com'
    username = "xyz@gmail.com"
    password = "blahblah"   # This is password to xyz@gmail.com

    msg = MIMEMultipart()
    msg['From'] = "xyz@gmail.com"
    msg['To'] = "abc@myNet.email.com"
    msg['Subject'] = "Re: abc Email Automation (Id=100)"
    msg['In-Reply-To'] = 904
    msg.attach(MIMEText(message))
    server = smtplib.SMTP(servername)
    try:
        server.set_debuglevel(True)
        server.ehlo()
        if server.has_extn('STARTTLS'):
            server.starttls()
            server.ehlo()  # re-identify ourselves over TLS connection

        server.login(username, password)
        server.sendmail(username, [to_email], msg.as_string())
    finally:
        print ("Can not send reply email")
        server.quit()

【问题讨论】:

    标签: python email


    【解决方案1】:

    我敢打赌你的端口错了。 https://support.google.com/a/answer/176600?hl=en 为您提供要使用的端口 - 我认为您在 SMTP 上使用 TLS,而不是 SSL(我知道,这令人困惑:P)所以您应该设置端口 587。

    server = smtplib.SMTP(servername, 587)
    

    【讨论】:

    • 谢谢我试过了:try: server = smtplib.SMTP(servername, 587) server.set_debuglevel(True) server.ehlo() if server.has_extn('STARTTLS'): server.starttls() server.ehlo() # 通过 TLS 连接重新识别自己 server.login(username, password) server.sendmail(username, [to_email], msg.as_string()) 仍然得到同样的错误
    • 谢谢丹.. 我设法让它工作,你是对的。这就是我所做的 gmail = smtplib.SMTP('smtp.gmail.com', 587) gmail.ehlo() gmail.starttls() gmail.login("xyz@gmail.com", "blahblah")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2023-04-06
    • 2014-11-01
    相关资源
    最近更新 更多