python发送qq邮件
时间: 2023-07-22 15:08:21 浏览: 55
你可以使用 Python 的 smtplib 模块来发送 QQ 邮件。以下是一个发送 QQ 邮件的示例代码:
```python
import smtplib
from email.mime.text import MIMEText
def send_qq_email(sender, password, receiver, subject, message):
# 邮件内容设置
msg = MIMEText(message, 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
# 连接 SMTP 服务器并发送邮件
try:
smtp_server = 'smtp.qq.com'
smtp_port = 465 # QQ邮箱的SMTP端口为465
smtp = smtplib.SMTP_SSL(smtp_server, smtp_port)
smtp.login(sender, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
print("邮件发送成功")
except Exception as e:
print("邮件发送失败:", str(e))
# 发件人邮箱和密码
sender_email = 'your_sender_email@qq.com'
sender_password = 'your_sender_password'
# 收件人邮箱
receiver_email = 'receiver_email@qq.com'
# 邮件主题和内容
email_subject = '测试邮件'
email_message = '这是一封测试邮件'
# 发送邮件
send_qq_email(sender_email, sender_password, receiver_email, email_subject, email_message)
```
请确保将 `your_sender_email@qq.com` 和 `your_sender_password` 替换为你自己的 QQ 邮箱地址和密码,将 `receiver_email@qq.com` 替换为接收邮件的邮箱地址。