一:发送文本信息
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
#qq服务器
host_server = 'smtp.qq.com'
#sender_qq为发件人的qq号码
sender_qq = '**263357**'
#qpass为qq邮箱的授权码
qpass = 'iajfnsdabvjxh***'
#发件人的邮箱
sender_qq_mail = '**2633573**@qq.com'
#收件人邮箱
receiver = '***5228***@qq.com'
#发送邮件的正文内容
mail_content = '邮箱测试'
#发送邮件标题
mail_title = 'python的邮件'
#ssl登录qq邮箱
smtp = SMTP_SSL(host_server)
smtp.set_debuglevel(1)
smtp.ehlo(host_server)
smtp.login(sender_qq,qpass)
#发送邮件
msg = MIMEText(mail_content, "plain", 'utf-8')
msg["Subject"] = Header(mail_title, 'utf-8')
msg["From"] = sender_qq_mail
msg["To"] = receiver
smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
smtp.quit()
二:发送邮件带附件
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from smtplib import SMTP_SSL
#qq邮箱smtp服务器
host_server = 'smtp.qq.com'
#sender_qq为发件人的qq号码
sender_qq = '**263357**'
#qpass为qq邮箱的授权码
qpass = 'iajfnsdabvjxh***'
#发件人的邮箱
sender_qq_mail = '**263357**@qq.com'
#收件人邮箱
receiver = '***6643***@qq.com'
#邮件的正文内容
mail_content = '邮箱测试'
#邮件标题
mail_title = 'python的邮件'
#ssl登录
smtp = SMTP_SSL(host_server)
smtp.set_debuglevel(1)
smtp.ehlo(host_server)
smtp.login(sender_qq,qpass)
msg= MIMEMultipart()
msg["Subject"] = Header(mail_title, 'utf-8')
msg["From"] = sender_qq_mail
msg["To"] = receiver
msg.attach(MIMEText(mail_content, "plain", 'utf-8'))
# 附件,当前目录下的 test.txt 文件
att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写
att1["Content-Disposition"] = 'attachment; filename="test.txt"'
msg.attach(att1)
smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
smtp.quit()