TypeError:无法连接电子邮件中的“ str”和“ list”对象

恩佐·维达尔(Enzo Vidal)

我正在使用python发送电子邮件。现在,我想通过电子邮件从列表中发送条目,但是遇到一个错误,提示“ TypeError:无法连接'str'和'list'对象”,我不知道要对其进行调试。以下是我的代码。我还是这个语言的新手(3周),所以有点背景知识。

import smtplib
x = [2, 3, 4] #list that I want to send
to = '' #Recipient
user_name = '' #Sender username
user_pwrd = '' #Sender Password
smtpserver = smtplib.SMTP("mail.sample.com",port)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(user_name,user_pwrd)

#Header Part of the Email       
header = 'To: '+to+'\n'+'From: '+user_name+'\n'+'Subject: \n'
print header

#Msg 
msg = header + x #THIS IS THE PART THAT I WANT TO INSERT THE LIST THAT I WANT TO SEND. the type error occurs in this line

#Send Email     
smtpserver.sendmail(user_name, to, msg)
print 'done!'

#Close Email Connection
smtpserver.close()
戴维·桑德斯

问题出在哪里msg = header + x您正在尝试将+运算符应用于字符串和列表。

我不确定您要如何x显示,但是,如果您想要类似“ [1、2、3]”的内容,则需要:

msg = header + str(x)

或者你可以做

msg = '{header}{lst}'.format(header=header, lst=x)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python TypeError:无法在邮件的电子邮件正文部分中串联“ str”和“ list”对象

Python:TypeError无法连接'str和'list'对象

Python TypeError:无法连接“ str”和“ list”对象

TypeError:无法连接“ str”和“ UUID”对象

TypeError:无法连接“ str”和“ Redditor”对象

无法连接'str'和'list'对象列表包含整数

TypeError:无法连接“ str”和“ float”对象:pandas

Python Selenium TypeError:无法连接“ str”和“ WebElement”对象

TypeError:无法连接“ str”和“ pygame.Surface”对象

Python:TypeError:无法连接“ str”和“ int”对象

re.search()TypeError:无法连接“ str”和“ NoneType”对象

TypeError:无法连接'str'和'function'对象的python文件

Tkinter Label,TypeError:无法连接“ str”和“ instance”对象

abaqus脚本:TypeError:无法连接“ str”和“ Set”对象

openerp无法连接“ str”和“ function”对象

TypeError:无法使用MySQL连接Flask中的'str'和'float'对象

如何连接str和int对象?

类型错误:无法连接“str”和“int”对象 1

rpsls游戏“(python)错误:无法连接'str'和'int'对象

类型错误无法连接“ str”和“ ErrorList”对象

JSON 对象必须是 str,而不是 'list' - TypeError

TypeError:无法连接类型为“ <class'str'>”的对象;仅Series和DataFrame objs有效

Python TypeError:尝试打印时无法连接“ str”和“ int”对象

TypeError:无法连接'str'和'NoneType'对象python bs4

类型错误:无法连接“str”和“list”对象。如何将列表转换为字符串

类型错误:无法使用 haarcascade 在人脸检测中连接“str”和“int”对象

TypeError:无法将“ float”对象隐式转换为str或TypeError:-:“ str”和“ float”的不受支持的操作数类型

Python:TypeError:无法连接“str”和“int”

TypeError:将自定义网址放入scrapy.Request()时,无法连接“ str”和“ NoneType”对象