‘list’ object has no attribute ‘encode’
Connection unexpectedly closed
出错代码
import smtplib
from email.mime.text import MIMEText
class SendEmial(object):
'''封装发送邮件类'''
def __init__(self,host:str,port:int,user:str,pwd:str):
self.host = host
self.port = port
self.user = user
self.pwd = pwd
def __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.user
msg["To"] = to_user
msg["subject"] = subject
self.__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")