import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
#登陆信息
host_server = 'smtp.qq.com' #使用的邮箱
sender = '' #发送人
code = '' #邮箱的授权码
receiver = '' #收件人
#邮件内容构造
mail_title = '实验' #标题
mail_content = '你好!' #内容
msg = MIMEMultipart() # 实例化
msg['Subject'] = mail_title # 标题
msg['From'] = sender # 发件人
msg['To'] = receiver # 收件人
msg.attach(MIMEText(mail_content, 'plain', 'utf-8')) # 文本内容,发送格式,编码格式
# 发送
try:
smtp = smtplib.SMTP(host_server)
smtp.login(sender, code) # 登陆
smtp.sendmail(sender,receiver,msg.as_string()) #发送信息
print(' 发送成功 ')
except smtplib.SMTPException:
print('发送异常,发送失败')