[英]Python Send reply to an gmail fails: A connection attempt failed because
Python sending a reply to an existing email in a gmail account fails with this error: 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
I have an email in my gmail account sent by an automation entity. 我的gmail帐户中有一封由自动化实体发送的电子邮件。 With my python code, I receive its UID and try to reply to that, but it fails. 使用我的python代码,我收到了它的UID并尝试对此进行回复,但失败了。
I read several stackoverflow posts on gmail reply and even read the https://tools.ietf.org/html/rfc2822#appendix-A.2 and Gmail Send Email As Reply 我读了一些关于Gmail回复的stackoverflow帖子,甚至阅读了https://tools.ietf.org/html/rfc2822#appendix-A.2和Gmail发送电子邮件作为回复
# 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()
I would bet you have the port wrong. 我敢打赌你的端口不对。 https://support.google.com/a/answer/176600?hl=en gives you the port to use - you're using TLS atop SMTP, not SSL I think ( I know, it's confusing :P) so you should be setting port 587. 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.