发送邮件
Posted luckiness
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了发送邮件相关的知识,希望对你有一定的参考价值。
import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication if __name__ == ‘__main__‘: fromaddr = ‘179xxx@qq.com‘ password = ‘xxxxxx‘ toaddrs = [‘179xxx@qq.com‘, ‘925xxx@qq.com‘] #构建文字内容 content = ‘hello, this is email content.‘ textApart = MIMEText(content,‘plain‘,‘utf-8‘) #读取本地图片 imageFile = ‘D:计算机二级dog.jpg‘ imageApart = MIMEImage(open(imageFile, ‘rb‘).read(), imageFile.split(‘.‘)[-1]) imageApart.add_header(‘Content-Disposition‘, ‘attachment‘, filename=imageFile) #读取本地文档 pdfFile = ‘D:计算机二级chicago.csv‘ pdfApart = MIMEApplication(open(pdfFile, ‘rb‘).read()) pdfApart.add_header(‘Content-Disposition‘, ‘attachment‘, filename=pdfFile) m = MIMEMultipart() m.attach(textApart) m.attach(imageApart) m.attach(pdfApart) m[‘Subject‘] = ‘title‘ #发送邮件 try: server = smtplib.SMTP(‘smtp.qq.com‘,25) server.login(fromaddr,password) server.sendmail(fromaddr, toaddrs, m.as_string()) print(‘success‘) server.quit() except smtplib.SMTPException as e: print(‘error:‘,e) #打印错误
以上是关于发送邮件的主要内容,如果未能解决你的问题,请参考以下文章