今天我带大家实现下简单的发邮件功能。我们要使用到email和smtplib库,这两个库时python自带的,直接import 引用就好了,实现环境python3.6.
- 对QQ邮箱进行开启SMTP服务
开启SMTP服务可以查看此链接,讲的很详细: https://jingyan.baidu.com/article/b0b63dbf1b2ef54a49307054.html
如下图:需要开启POP3/SMTP 服务,获取到的授权码,就是你登陆时需要用到的。
- 使用QQ邮箱进行邮件发送:
第一步:登陆QQ邮箱。
第二步:构建发件人,收件人,邮件标题,邮件内容等信息。
第三步:发送邮件。
1 import smtplib 2 import email 3 from email.mime.text import MIMEText 4 from email.header import Header 5 6 7 def email_sendmail(sender,receiver,title,message): 8 username = "[email protected]" 9 password_pop3 = "veixjxlmxxxxbdgb" 10 #password_imap = "wpxlacxxxxgpbcje" 11 smtp = smtplib.SMTP() 12 smtp.connect('smtp.qq.com') 13 smtp.login(username, password_pop3) 14 msg = MIMEText(message, "plain", 'utf-8') 15 msg["Subject"] = Header(title, 'utf-8') 16 msg["From"] = sender 17 msg["To"] = receiver 18 smtp.sendmail(sender, receiver, msg.as_string()) 19 smtp.quit() 20 21 if __name__ == "__main__": 22 23 sender = "[email protected]" 24 receiver = "[email protected]" 25 mail_message = 'Dear all,\n 今日CP3良率监控情况如下所示:\n 您们好,我是TE助理1号,很高兴加入TE group,我会努力为大家提供服务,希望大家能够喜欢我,谢谢!\n' 26 mail_title = "CP3 yield monitor" 27 28 email_sendmail(sender,receiver,mail_title,mail_message) #登陆QQ邮箱并发送消息