python学习笔记——邮件发送

#coding = utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
_user = "[email protected]"
_pwd = "fwtelthkzrltdhgd"
_to = "[email protected]"
msg = MIMEMultipart()
msg["Subject"] = "测试一下"
msg["From"] = _user
msg["To"] = _to
part = MIMEText("Python 发送邮件测试")
msg.attach(part)

part =MIMEApplication(open('D:\\stacy\\工作\\Charge-C.txt','rb').read())
part.add_header('Content-Disposition','attachment',filename='Charge-C.txt')
msg.attach(part)

try:
    s = smtplib.SMTP_SSL("smtp.qq.com",465)
    s.login(_user,_pwd)
    s.sendmail(_user,_to,msg.as_string())
    s.quit()
    print("完美!")

except smtplib.SMTPException as e:
    print("失败!")

猜你喜欢

转载自www.cnblogs.com/stacy828/p/9947933.html