‘list’ object has no attribute ‘encode’
Connection unexpectedly closed
出错代码
import smtplib
from email.mime.text import MIMETextclass SendEmial(object):'''封装发送邮件类'''def __init__(self,host:str,port:int,user:str,pwd:str):self.host = hostself.port = portself.user = userself.pwd = pwddef __send(self,msg): #私有方法try:smtpObj = smtplib.SMTP()smtpObj.connect(self.host,self.port) #连接服务器smtpObj.login(self.user,self.pwd) # 登录smtpObj.sendmail(self.user,msg['To'],msg.as_string()) # 发送邮件except Exception as e:print(e)def send_text(self,to_user:str, context:str, subject:str):msg = MIMEText(context,_subtype='plain',_charset='utf-8')msg["Form"] = self.usermsg["To"] = to_usermsg["subject"] = subjectself.__send(msg)
if __name__ == '__main__':sendEmial = SendEmial(host="smtp.qq.com",port="465",user="197889917@qq.com",pwd="ocdccawxrbvxbhfg")sendEmial.send_text(to_user=["197889917@qq.com"], context="cesiqq", subject="ces")