Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AttributeError:SMTP#U SSL实例没有属性'__退出';_Python_Ssl_Server_Smtp_Attributeerror - Fatal编程技术网

Python AttributeError:SMTP#U SSL实例没有属性'__退出';

Python AttributeError:SMTP#U SSL实例没有属性'__退出';,python,ssl,server,smtp,attributeerror,Python,Ssl,Server,Smtp,Attributeerror,我知道还有其他类似的问题,但我遵循了答案,仍然有相同的错误,我的代码是: import csv, smtplib, ssl message = """Subject: Test Hi {name}""" from_address = "random@gmail.com" password = "pasword" context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465) as s

我知道还有其他类似的问题,但我遵循了答案,仍然有相同的错误,我的代码是:

import csv, smtplib, ssl

message = """Subject: Test

Hi {name}"""
from_address = "random@gmail.com"
password = "pasword"



context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
    server.login(from_address, password)
    with open("contacts_file.txt") as file:
        reader = csv.reader(file)
        next(reader)
        next(reader) 
        for number, name, email in reader:
            server.sendmail(
                from_address, email, message.format(name=name)
            )
        server.quit()

谢谢大家!

使用的smtplib版本不支持上下文管理器。很可能您使用的Python版本低于3.3

为了理解错误,我创建了这两个类,并将其与文本文件hello.txt一起保存。第一个类不支持上下文管理器,它将引发与您所拥有的类似的错误,而第二个类不支持


类OpenWithOutExit:
'''
该类没有用于创建上下文管理器的enter和exit的特殊dunder
'''
定义初始化(self,file,mode='r'):
self.data=打开(文件,模式)
def关闭(自我):
self.data.close()
类OpenWithExit:
'''
此类具有用于创建上下文管理器的输入和输出的特殊dunder
'''
定义初始化(self,file,mode='r'):
self.file=文件
self.mode=mode
定义输入(自我):
self.data=open(self.file,self.mode)
返回self.data
定义退出(自身、异常类型、异常值、回溯):
self.data.close()
打印('我们存在没有问题')
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
#使用类上下文管理器
尝试:
使用OpenWithExit('hello.txt')作为f:
#做点什么
通过
除AttributeError外,其他属性为:
打印('这不会打印')
#使用没有上下文管理器的类
尝试:
将OpenWithOutExit('hello.txt')作为f:
#做点什么
通过
除属性错误为e外:
打印('这将导致属性退出错误')
提高e

您使用的python版本是什么<在3.3版本的smtplib中添加了带有的代码>支持,非常感谢!我使用较旧版本的python运行该文件。