python实战===使用smtp发送邮件的源代码,解决554错误码的问题
import smtplib from email.mime.text import MIMEText from email.header import Header import time def email(): #发送邮箱服务器 smtpserver = "smtp.163.com" #发送邮箱的账号/密码 user= "我的邮箱@163.com" password="我的密码" #发送邮箱 sender="我的邮箱@163.com" #收件箱 receiver = "你的邮箱@qq.com" #发送主题 subject = "《脑筋急转弯》" #编写HTML类型的邮件正文 msg = MIMEText("<html><h1>ssssssssssssssss</h1></html>","html","utf-8") msg['Subject'] = Header(subject, 'utf-8') try: #这两个参数必须要,不然就会出现554的错误,不然少参数 msg['from']=sender msg['to']=receiver #连接发送邮箱 smtp = smtplib.SMTP() smtp.connect(smtpserver) smtp.login(user,password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit() print("执行第",i,"次,成功!") time.sleep(2) except: print("第",i,"次发送失败!") i=1 while i: email() i +=1 if i >3: break
纸上得来终觉浅,绝知此事要躬行!