前言
其实Python使用QQ邮箱发送Email代码很简单,短短几行代码就可以实现这个功能。
使用到的模块有smtplib和email这个两个模块,关于这两个模块的方法就不多说了。不了解的朋友们可以查看这篇文章:python中使用smtplib和email模块发送邮件实例
我们先说说网上常用的使用这那两个模块发送邮件的方法
代码如下:
import smtplib from email.mime.text import MIMEText from email.header import Header def SendEmail(fromAdd, toAdd, subject, attachfile, htmlText): strFrom = fromAdd; strTo = toAdd; msg =MIMEText(htmlText); msg['Content-Type'] = 'Text/HTML'; msg['Subject'] = Header(subject,'gb2312'); msg['To'] = strTo; msg['From'] = strFrom; smtp = smtplib.SMTP('smtp.qq.com'); smtp.login('501257367@qq.com','password'); try: smtp.sendmail(strFrom,strTo,msg.as_string()); finally: smtp.close; if __name__ == "__main__": SendEmail("501257367@qq.com","501257367@qq.com","","hello","hello world");
运行结果:
smtplib.SMTPAuthenticationError: (530, 'Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help"color: #800000">报错,需要一个安全的连接,例如SSL,因此接下来我们会使用SSL的方式去登录,但是在那之前,我们需要做一些准备,打开qq邮箱,点击设置->
账户,找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,开启IMAP/SMTP服务,然后根据要求使用手机发送到指定号码,获取授权码,
这个授权码就是你接下来登录要使用的密码,配置完成,上代码
import smtplib from email.mime.text import MIMEText _user = "你的qq邮箱" _pwd = "你的授权码" _to = "501257367@163.com" msg = MIMEText("Test") msg["Subject"] = "don't panic" msg["From"] = _user msg["To"] = _to try: s = smtplib.SMTP_SSL("smtp.qq.com", 465) s.login(_user, _pwd) s.sendmail(_user, _to, msg.as_string()) s.quit() print "Success!" except smtplib.SMTPException,e: print "Falied,%s"%e运行结果如下:
总结
好了,大功告成!以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能带来一定的帮助,如果有疑问大家可以留言交流。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 《腾格尔 容中尔甲 亚东 高原三星 男人篇 3CD》[WAV/分轨][1GB]
- 命运圣契公测实测可用兑换码大全 命运圣契最新兑换码分享
- 黑神话悟空上品疾蝠精魄获取方法一览|上品疾蝠精魄收集攻略
- 《七龙珠电光炸裂!ZERO》GT角色预告片曝光,15位新角色登场
- [ABC]安娜-胆麦发烧女声[6N纯银镀膜][2016[低速原抓WAV+CUE]
- NewViennaOctetViennaWindSoloists-TheDeccaRecordings(2024)18CD[24-48][FLAC]-7
- 雨果唱片-三角琴《俄罗斯民间音乐系列-梁祝(中国名曲)》wav
- 《塞尔达传说:智慧的再现》新实机!无之世界介绍
- 任天堂公布《塞尔达传说》系列时间线:野炊与王泪独立在外
- 任天堂更新网络投稿规范:打击影响他人游戏体验行为
- 冼佩瑾.2024-女儿Daughter(EP)【杰威尔】【FLAC分轨】
- 黑豹乐队.1993-光芒之神【摇音晨【WAV+CUE】
- 曾轶可.2011-一只猫的旅行【天娱传媒】【WAV+CUE】
- 黑神话悟空上品狼刺客精魄获取方法一览|上品狼刺客精魄收集攻略
- 视觉小说《神椿市建设中REGENERATE》将于2025年2月20日发售