python发送qq邮件
时间: 2023-07-22 10:08:21 浏览: 109
要使用Python发送QQ邮件,你需要使用smtplib库和QQ邮箱的SMTP服务器。下面是一个简单的示例代码:
```python
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 发件人邮箱
sender = 'your_email@qq.com'
# 收件人邮箱
receiver = 'recipient_email@qq.com'
# QQ邮箱授权码(需要在QQ邮箱设置中开启SMTP服务获取)
password = 'your_qq_email_password'
# 邮件内容
subject = 'Python邮件测试'
content = '这是一封通过Python发送的邮件。'
# 构造邮件对象
message = MIMEText(content, 'plain', 'utf-8')
message['From'] = Header(sender, 'utf-8')
message['To'] = Header(receiver, 'utf-8')
message['Subject'] = Header(subject, 'utf-8')
try:
# 连接QQ邮箱的SMTP服务器
server = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 登录QQ邮箱
server.login(sender, password)
# 发送邮件
server.sendmail(sender, receiver, message.as_string())
print('邮件发送成功')
except Exception as e:
print('邮件发送失败:', str(e))
finally:
# 关闭连接
server.quit()
```
请替换代码中的`your_email@qq.com`为发件人邮箱地址,`recipient_email@qq.com`为收件人邮箱地址,`your_qq_email_password`为QQ邮箱授权码。
注意:为了使用QQ邮箱的SMTP服务,需要在QQ邮箱的设置中开启SMTP服务,并获取授权码。
阅读全文