使用Python发送邮件

爱⌒轻易说出口 提交于 2019-12-26 15:46:31
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart #多媒体处理模块
from email.header import Header

mail_host = "smtp.qq.com"
mail_user = "888888@qq.com"
mail_pass = "*******" #授权码

sender = "888888@qq.com"
receiver = ["888888@qq.com"]
textContent = MIMEText('这个邮件里面展示了我用四行代码给图片加一个滤镜的功能','plain','utf-8')
textContent['From'] = Header("夜曲编程",'utf-8')
textContent['To'] = Header("测试",'utf-8')

imageFile = './/image//01.jpg'
imageAttachment = MIMEImage(open(imageFile,'rb').read(),'jpeg')
imageAttachment.add_header('Content-Disposition','attachment',filename=imageFile)

imageFile2 = '郭麒麟.jpg'
imageAttachment2 = MIMEImage(open(imageFile2,'rb').read(),'jpeg')
imageAttachment2.add_header('Content-Disposition','attachment',filename=imageFile2)

message = MIMEMultipart()
message.attach(textContent)
message.attach(imageAttachment)
message.attach(imageAttachment2)

subject = '夜曲编程pythonSMTP邮件测试'
message['Subject'] = Header(subject,'utf-8')

smtpObj = smtplib.SMTP_SSL(mail_host)
smtpObj.connect(mail_host,465)
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(sender,receiver,message.as_string())
print("发送邮件成功")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!