首页 > 科技 > > 正文
2025-03-28 11:15:27

🌟Python QQ 发送Email SMTP📧

导读 在数字化时代,自动化工具让生活更高效!今天分享一个实用的小技巧——用Python通过QQ邮箱发送邮件。💡这个功能基于SMTP协议,简单易学,适

在数字化时代,自动化工具让生活更高效!今天分享一个实用的小技巧——用Python通过QQ邮箱发送邮件。💡这个功能基于SMTP协议,简单易学,适合处理日常事务或批量通知任务。

首先,你需要登录QQ邮箱,进入设置→账户,找到“POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务”,开启SMTP服务,并获取授权码(不是密码)。📱接着,在Python环境中安装`smtplib`和`email`库,编写如下代码:

```python

import smtplib

from email.mime.text import MIMEText

配置信息

smtp_server = 'smtp.qq.com'

sender_email = '你的QQ邮箱@qq.com'

receiver_email = '接收者邮箱'

password = '授权码'

创建邮件内容

msg = MIMEText('Hello, this is a test email from Python!', 'plain', 'utf-8')

msg['From'] = sender_email

msg['To'] = receiver_email

msg['Subject'] = 'Python 发送测试邮件'

发送邮件

try:

server = smtplib.SMTP_SSL(smtp_server, 465)

server.login(sender_email, password)

server.sendmail(sender_email, [receiver_email], msg.as_string())

print("邮件发送成功!")

except Exception as e:

print(f"邮件发送失败: {e}")

finally:

server.quit()

```

运行后,你就能轻松实现邮件自动发送啦!🙌快来试试吧,无论是工作汇报还是个人提醒,都能事半功倍哦~