繁体   English   中英

Python发送对Gmail消息的回复,得到此错误:AttributeError:'int'对象没有属性'encode'

[英]Python send reply to a gmail message getting this error: AttributeError: 'int' object has no attribute 'encode'

我已经构建了以下代码来回复我的gmail帐户中的现有电子邮件

replySubj = "Re: hello"
recipient = "test@acc.emailTest.com"
sender = "dummy1@gmail.com"
username = "dummy1@gmail.com"
password = "blahblahblah"

msg = MIMEMultipart('')
msg['From'] = sender
msg['To'] = recipient
msg['Subject'] = replySubj
msg['In-Reply-To'] = uid
msg.attach(MIMEText("I received your message"))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(sender, recipient, msg.as_string())
server.quit()

我不断收到此错误-> AttributeError:'int'对象没有与此行相关的属性'encode'

server.sendmail(sender, recipient, msg.as_string())

当我删除.as_string()时,出现以下错误:TypeError:预期的字符串或类似字节的对象

请注意,uid是整数

感谢“ entrez”的提示,解决方案是

msg ['In-Reply-To'] = str(uid)

我以某种方式认为我做了:msg.as_string()它会使整个字符串变成字符串,但是由于uid是整数,因此我必须先将其转换为字符串

暂无
暂无

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

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