当我使用它来发送txt文件,图像或音频时,我有下面的代码可以正常工作。但是,当我尝试发送zip文件,rar文件或任何其他没有自己MIME(与MIMEText,MIMEImage或MIMEAudio无关)的文件时,它不起作用。
总之,每当我到达else部分(MIMEBase命令)时,我都会做错事并得到错误:
e.send_mail(TARGET, SUBJECT, "file.zip")
msg.attach(part) //two lines after the else's end
AttributeError: 'str' object has no attribute 'append'
代码:
def send_mail(self, target, subject, *file_names):
"""
send a mail with files to the target
@param target: send the mail to the target
@param subject: mail's subject
@param file_names= list of files to send
"""
msg = email.MIMEMultipart.MIMEMultipart()
msg['From'] = self.mail
msg['To'] = email.Utils.COMMASPACE.join(target)
msg['Subject'] = subject
for file_name in file_names:
f = open(file_name, 'rb')
ctype, encoding = mimetypes.guess_type(file_name)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
# in case of a text file
if maintype == 'text':
part = MIMEText(f.read(), _subtype=subtype)
# in case of an image file
elif maintype == 'image':
part = MIMEImage(f.read(), _subtype=subtype)
# in case of an audio file
elif maintype == 'audio':
part = MIMEAudio(f.read(), _subtype=subtype)
# any other file
else:
part = MIMEBase(maintype, subtype)
msg.set_payload(f.read())
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file_name))
msg.attach(part)
f.close()
# ssl server doesn't support or need tls, so don't call server_ssl.starttls()
self.server_ssl.sendmail(self.mail, target, msg.as_string())
#server_ssl.quit()
self.server_ssl.close()
我见过类似的代码,但我不明白我的错误。
你可以解释一下我搞砸了什么吗? 谢谢你!答案 0 :(得分:0)
if it helps anyone here is the answer: the main problem was that I changed the msg payload instead of the zip file's
<ListView
android:id="@+id/listDevicesMain"
android:layout_width="match_parent"
android:layout_height="match_parent" />