from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import smtplib from email.mime.image import MIMEImage
def send_mail(self): mail_host = "smtp.qq.com" # SMTP服务器地址 mail_sender = "******@qq.com" # 发送人账号 mail_receiver ="*****@qq.com" #接收人账号 msg = MIMEMultipart() msg["Subject"] = "带有附件的邮件" msg["From"] = mail_sender # 发送人 msg["To"] = mail_receiver # 接收人账号 # 邮件正文 content = ''' 这是一封带有附件的邮件... 有两个附件 ''' msg.attach(MIMEText(content, 'plain', 'utf-8')) # 构造附件html文件 att2 = MIMEText(open('report.html', 'rb').read(), 'base64', 'utf-8') att2["Content-Type"] = 'application/octet-stream' att2["Content-Disposition"] = 'attachment; filename="report.html"' msg.attach(att2) ## 发送邮件 s = smtplib.SMTP('smtp.qq.com') # 实例化对象 s.connect('smtp.qq.com', 25) # 连接163邮箱服务器,端口号为25 s.login(mail_sender, "gxxinpgfgxnwhiej") # 登录邮箱,gxxinpgfgxnwhiej管理密码,不是登录密码 s.sendmail(mail_sender, [ mail_receiver], msg.as_string()) s.quit()