python发送QQ邮件 python实现发送QQ邮件(可加附件)
舒然---小广广 人气:0东西比较简单,简单讲一下,直接贴代码了,其他邮箱都类似。
1.首先在qq 邮箱里面把stmp服务 打开
2.拉到下面,开启第一个,发送短信验证后会得到一个授权码:
3.代码,要注意的地方我都贴了注释:
# coding=utf-8 import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication #写成了一个通用的函数接口,想直接用的话,把参数的注释去掉就好 def sen_email(msg_from,passwd,msg_to,text_content,file_path=None): #msg_from = '1095133888@qq.com' # 发送方邮箱 #passwd = 'zjvoymwngfhigjss' # 填入发送方邮箱的授权码(就是刚刚你拿到的那个授权码) #msg_to = '1095133998@qq.com' # 收件人邮箱 msg = MIMEMultipart() subject = "Test My Email" # 主题 #text_content = "你好啊,你猜这是谁发的邮件" text = MIMEText(text_content) msg.attach(text) #docFile = 'C:/Users/main.py' 如果需要添加附件,就给定路径 if file_path: #最开始的函数参数我默认设置了None ,想添加附件,自行更改一下就好 docFile = file_path docApart = MIMEApplication(open(docFile, 'rb').read()) docApart.add_header('Content-Disposition', 'attachment', filename=docFile) msg.attach(docApart) msg['Subject'] = subject msg['From'] = msg_from msg['To'] = msg_to try: s = smtplib.SMTP_SSL("smtp.qq.com", 465) s.login(msg_from, passwd) s.sendmail(msg_from, msg_to, msg.as_string()) print "发送成功" except smtplib.SMTPException, e: print "发送失败" finally: s.quit()
加载全部内容