假设我们要向一个名为example.docx的Word文档中添加一个名为attachment.txt的附件,代码如下: fromdocximportDocumentfromdocx.oxmlimportOxmlElement# 打开Word文档doc=Document('example.docx')# 添加附件attachment=doc.element.body.insert(0,OxmlElement('w:p'))run=attachment.add_run()run.add_text('Attachme...
创建一个脚本,命名为add_attachment.py,并在其中添加以下代码。 import osimport smtplibfrom email.mime.text import MIMETextfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartimport getpasshost_name = 'smtp.gmail.com'port = 465sender = 'sender_emailid'password = getpa...
email_file=MIMEApplication(open('F:\\temp.html', 'rb').read())#第一个参数打开文件read()方法读出所有内容,刚好是字符串格式,第二个参数是希望的编码,这种方法比较简单 email_file.add_header('Content-Disposition', 'attachment', filename='temp.html')#这里添加一个标题,Content-Disposition,attachment...
插入Python对象:attachment = "路径/文件名" # Python对象的路径或文件名 mail.Attachments.Add(attachment) 设置收件人、抄送、密送等信息:mail.To = "收件人邮箱" mail.CC = "抄送邮箱" mail.BCC = "密送邮箱" 发送邮件:mail.Send() 通过以上步骤,可以使用Python脚本在Outlook中发送电子邮件,并插入Python对象...
add_header('Content-Disposition', "attachment; filename= %s" % filename) msg.attach(part) # 连接到SMTP服务器 try: server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() # 启用TLS server.login(sender_email, sender_password) # 登录 # 合并收件人和抄送人列表 all_recipients = ...
attachment=open("path_to_attachment_file","rb")part=MIMEBase("application","octet-stream")part.set_payload((attachment).read())encoders.encode_base64(part)part.add_header("Content-Disposition","attachment; filename=attachment_file_name")msg.attach(part) ...
()) TXT.add_header('Content-Disposition', 'attachment', filename='TXT.txt') #添加一个video #但是接受到的时候文件是.bin后缀名,需要手动改成MP4后缀 # VIDEO = MIMEApplication(open('video.mp4','rb').read()) # VIDEO.add_header("Conten-Disposition",'attachment',filename = 'video.mp4') ...
attachment.add_header('Content-Disposition', 'attachment', filename='attachment.pdf') 将附件添加到新的MIMEMultipart对象中: 代码语言:txt 复制 new_msg.attach(attachment) 将新的MIMEMultipart对象写入新的eml文件: 代码语言:txt 复制 with open('new.eml', 'wb') as f: f.write(new_msg.as_bytes...
part.add_header('Content-Disposition', 'attachment', filename="abc.docx") message.attach(part) smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25) smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) ...
5. 通过Attachments.Add()方法添加附件并获取附件对象。 6. 通过attachment.PropertyAccessor.SetProperty()方法将附件对象嵌入邮件正文中,并将src属性的值设为cid:image1。 7. 设置邮件的html格式正文。 8. 使用mail.Send()方法发送邮件。