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

时间:2019-01-04 16:33:00

标签: python email smtp

我构建了以下代码以回复我的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是整数

1 个答案:

答案 0 :(得分:0)

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

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

我想我是怎么做的:msg.as_string()它会使整个字符串变成字符串,但是由于uid是整数,因此我必须先将其转换为字符串