python 企业qq邮箱发邮件
时间: 2024-08-17 19:00:16 浏览: 61
在Python中使用企业QQ邮箱发送邮件通常需要通过SMTP协议来实现,由于腾讯的企业邮箱服务通常有安全机制,如SMTP验证和SSL/TLS加密,所以你需要准备相应的授权信息以及设置合适的环境。以下是一个基本步骤的示例:
```python
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 邮箱账号信息
QQ_EMAIL = 'your_email@qq.com'
QQ_PASSWORD = 'your_qq_password'
# 收件人、主题和正文内容
to_email = 'recipient_email@qq.com'
subject = '测试邮件'
body = '这是一封来自Python的测试邮件。'
# 创建MIMEText对象
message = MIMEText(body, 'plain', 'utf-8')
message['From'] = Header(QQ_EMAIL)
message['To'] = Header(to_email)
message['Subject'] = Header(subject)
try:
# 使用SMTP_SSL连接企业QQ邮箱服务器(465端口)
smtp_server = "smtp.qq.com"
server = smtplib.SMTP_SSL(smtp_server, 465)
# 登录邮箱
server.login(QQ_EMAIL, QQ_PASSWORD)
# 发送邮件
server.sendmail(QQ_EMAIL, to_email, message.as_string())
print("邮件发送成功")
except Exception as e:
print(f"邮件发送失败:{str(e)}")
finally:
# 关闭连接
server.quit()
```
注意,你需要替换`your_email@qq.com`和`your_qq_password`为你自己的QQ邮箱账号和密码,并确保已经开启了SMTP服务。同时,如果你的邮箱设置了“开启二次验证”,则可能需要使用第三方库(如`smtplib2`)配合OAuth授权。
阅读全文
相关推荐

















