AttributeError: module 'smtplib' has no attribute 'SMTP_SSL'
时间: 2023-11-26 07:48:57 浏览: 82
这个错误通常是由于导入的smtplib模块中没有SMTP_SSL属性引起的。这可能是因为您的Python版本太旧,不支持SMTP_SSL。您可以尝试使用SMTP类而不是SMTP_SSL类来发送电子邮件。以下是一个示例代码片段,演示如何使用SMTP类发送电子邮件:
```python
import smtplib
from email.mime.text import MIMEText
mail_host = "smtp.qq.com"
mail_user = "your_email_address"
mail_pass = "your_email_password"
sender = 'your_email_address'
receivers = ['receiver_email_address']
message = MIMEText('This is a test email.')
message['From'] = sender
message['To'] = receivers[0]
message['Subject'] = 'Test Email'
try:
smtpObj = smtplib.SMTP(mail_host, 25)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("Email sent successfully")
except smtplib.SMTPException as e:
print("Error: unable to send email", e)
```
阅读全文