邮件发送py

发送QQ邮件之前,要先开启QQ邮箱IMAP/SMTP服务,并且记住授权码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import smtplib
from email.mime.text import MIMEText
mail_sender='发送方qq号@qq.com' #发送方邮箱
mail_code='发送方授权码' #授权码
mail_receivers='接收方qq号@qq.com' #邮件接收方
subject='测试Python发邮件' #主题
content='测试Python发邮件' #正文
msg=MIMEText(content,_charset='utf-8')
msg['Subject']=subject
msg['From']=mail_sender
msg['To']=mail_receivers
mail= smtplib.SMTP_SSL("smtp.qq.com", 465) #连接邮件服务器
mail.login(mail_sender, mail_code) #登录
mail.sendmail(mail_sender, mail_receivers, msg.as_string()) #发送邮件