python调qq邮箱
时间: 2024-03-13 21:41:01 浏览: 62
python实现QQ邮箱发送邮件
Python可以通过smtplib库来实现发送邮件的功能,而调用QQ邮箱的SMTP服务器可以使用QQ邮箱提供的SMTP服务。下面是一个简单的示例代码来演示如何使用Python发送邮件到QQ邮箱:
```python
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 邮件服务器的地址和端口
smtp_server = 'smtp.qq.com'
smtp_port = 465
# 发件人邮箱和密码
sender_email = 'your_sender_email@qq.com'
sender_password = 'your_sender_password'
# 收件人邮箱
receiver_email = 'your_receiver_email@qq.com'
# 邮件内容
subject = 'Python发送邮件示例'
content = '这是一封通过Python发送的邮件。'
# 创建邮件对象
message = MIMEText(content, 'plain', 'utf-8')
message['From'] = Header(sender_email, 'utf-8')
message['To'] = Header(receiver_email, 'utf-8')
message['Subject'] = Header(subject, 'utf-8')
try:
# 连接邮件服务器
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
server.login(sender_email, sender_password)
# 发送邮件
server.sendmail(sender_email, receiver_email, message.as_string())
print('邮件发送成功')
except Exception as e:
print('邮件发送失败:', str(e))
finally:
# 关闭连接
server.quit()
```
请注意,上述代码中的`your_sender_email@qq.com`和`your_sender_password`需要替换为你自己的QQ邮箱地址和密码,`your_receiver_email@qq.com`需要替换为你要发送邮件的收件人邮箱地址。
阅读全文