6-2邮件发送的课堂练习如下,其中发送邮箱设置为qq运行后提示“error:Connection unexpectedly closed”,帮看下什么原因?
smtplib email.mime.text MIMEText email.header Header mail_host = mail_user = mail_pass = sender = receivers = message = MIMEText(,,) message[] = Header(sender) message[] = Header(, ) : smtpobj = smtplib.SMTP() smtpobj.connect(mail_host,) smtpobj.login(mail_user,mail_pass) smtpobj.sendmail(sender,receivers,message.as_string()) smtplib.SMTPException e: (% e)
11
收起
正在回答 回答被采纳积分+1
2回答
yixiu0199
2023-04-21 20:46:07
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # coding: utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header #第三方的smtp服务. ?hotmail与qq邮箱发送都有问题待解决 mail_host = 'smtp-mail.outlook.com' # 设置服务器为自己hotmail邮箱的 mail_user = 'Zhang Ming' #自己的邮箱,要在设置里查看 mail_pass = '*******' #是自己邮箱密码,或授权码,要根据邮箱来定;找到smtp开通,是否存在授权码 sender = 'kzzjqs10@hotmail.com' #发送者信息,同line9 receivers = '2356947266@qq.com' #收件方信息 message = MIMEText( '这是一个Python邮件发送测试' , 'plain' , 'utf-8' ) #定义message的内容、类型、格式 message[ 'From' ] = Header(sender) #邮件来源 message[ 'Subject' ] = Header( 'python脚本测试' , 'utf-8' ) #邮件主题 #print(message.as_string()) #可打印加密的message字符串,看长什么样 #以上准备好后,下面就可以发送邮件了 try : smtpobj = smtplib.SMTP() #实例化(创建)邮件对象 smtpobj.connect(mail_host, 587 ) # 创建链接, 此处qq邮箱的端口号是465,或587 smtpobj.login(mail_user,mail_pass) smtpobj.sendmail(sender,receivers,message.as_string()) except smtplib.SMTPException as e: print ( 'error:%s' % e) |
Python全能工程师
- 参与学习 人
- 提交作业 16330 份
- 解答问题 4470 个
全新版本覆盖5大热门就业方向:Web全栈、爬虫、数据分析、软件测试、人工智能,零基础进击Python全能型工程师,从大厂挑人到我挑大厂,诱人薪资在前方!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧