python - 如何解决AttributeError : SMTP_SSL instance has no attribute '__exit__' in Python?

标签 python email ssl python-2.x smtplib

谁能帮我解决这个错误:AttributeError: SMTP_SSL instance has no attribute 'exit'。

我正在开发一个使用 Python 发送多封电子邮件的小模块。 python 版本:2.7.15 操作系统:MacOS X

import smtplib
import ssl
port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "abc@gmail.com"  # type: str # Enter your address
receiver_email = "xyz@gmail.com"  # Enter receiver address
password = 'xyz@0101'
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context ) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, message, receiver_email)

最佳答案

在Python3.3中添加了对with语句与smtplib.SMTP_SSL的支持,因此它在Python2.7中不起作用

像这样的东西应该可以工作:

context = ssl.create_default_context()
server = smtplib.SMTP_SSL(smtp_server, port, context )
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
server.quit()

关于python - 如何解决AttributeError : SMTP_SSL instance has no attribute '__exit__' in Python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53943240/

相关文章:

python - 在python中使用自定义命令制作数据库

email - 从历史上看,为什么有关电子邮件地址的RFC如此复杂?

wordpress - htaccess 需要重定向主域而不是附加域

c - 在windows上添加openssl c语言库

java - 如何使用 Apache HttpClient 4 添加对已弃用的 SSL 密码套件的支持

具有两个可能值的 IF block 的 Pythonic 处理

python - Pandas,跳过 xlsx 中的空列

sql - 同一查询中的多个通配符计数

asp.net - 如何从 MSN、Twitter、Facebook、GMail 等中检索联系人/电子邮件?

python - 如何使用 asyncio 在 python3 中运行并行作业?