繁体   English   中英

Python发送对gmail的回复失败:连接尝试失败,因为

[英]Python Send reply to an gmail fails: A connection attempt failed because

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://tools.ietf.org/html/rfc2822#appendix-A.2Gmail发送电子邮件作为回复

# 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()

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

server = smtplib.SMTP(servername, 587)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM