import smtplib #python自带
from email.mime.text import MIMEText
from email.header import Header
class Sendemail():
def sendqqemail(self):
emailo=smtplib.SMTP() #SMTP类的实例化对象
emailo.connect('smtp.qq.com',25) #smtp的默认端口号
sendmsg_contents=input('请输入邮件的文字内容:')
try:
emailo.login('46080****@qq.com','oimmpyaa****bjbe') #oimmpyaa****bjbe是发件人的邮箱的16位授权码,在邮箱的设置-账户-pop3/smtp中发送短信获取。
mg= MIMEText(f'<html><h1>{sendmsg_contents}</h1></html>','html','utf-8')#MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型
subject = '666python学习-爱软测同学们使用QQ邮箱给163邮箱发消息'
mg['subject'] = Header(subject,'utf-8')
mg['from'] = '46080****@qq.com'
mg["to"] = 'cherry****@163.com'#mg['subject'],mg['from'],mg["to"] 这三个为固定格式,不能少,依次为邮件主题,发件人,收件人
emailo.sendmail(mg['from'],mg["to"],mg.as_string()) #邮件内容必须为字符串格式,as_string()方法不能少
except SMTPAuthenticationError as e1:
print('邮件的授权码填写错误{}'.format(e1))
except SMTPDataError as e2:
print('邮件标题,收件人,发件人数据格式错误{}'.format(e2))
finally:
print('邮件发送成功!!')
emailo.quit()
if __name__ == '__main__':
s=Sendemail()
s.sendqqemail()