# coding: utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header # 发信方的信息:发信邮箱,QQ邮箱授权码 class QQemail: def __init__(self,to_addr = '*70***@qq.com'): # 发信方邮箱 self.from_addr = '*70***@qq.com' self.password = 'ddjenwuxcvkmbgbh' # 收信方邮箱 self.to_addr = to_addr self.F = smtplib.SMTP_SSL('smtp.qq.com',465) def message(self,test,header): # 邮箱正文内容,第一个参数为内容,第二个参数为格式(plain 为纯文本),第三个参数为编码 msg = MIMEText(test,'plain','utf-8') # 邮件头信息 msg['From'] = Header(self.from_addr) msg['To'] = Header(','.join(self.to_addr)) msg['Subject'] = Header(header) return msg def send(self,test,header): msg = self.message(test,header) # 登录发信邮箱 self.F.login(self.from_addr,self.password) # 发送邮件 try: self.F.sendmail(self.from_addr,self.to_addr,msg.as_string()) print ('邮件发送成功') except: print ('邮件发送失败') # 关闭服务器 self.F.quit() if __name__ == '__main__': #邮件正文 test='python发送QQ邮箱测试' header = 'testpython发送QQ邮箱' QQ=QQemail() QQ.send(test,header)