码迷,mamicode.com
首页 > 其他好文 > 详细

发送邮件

时间:2020-07-24 21:35:46      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:email   word   str   erro   err   xxxxxx   his   pex   pass   

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) #打印错误

 

发送邮件

标签:email   word   str   erro   err   xxxxxx   his   pex   pass   

原文地址:https://www.cnblogs.com/luckiness/p/13374216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!