python发送邮件
时间: 2023-08-19 20:16:54 浏览: 131
python 发送邮件
Python发送邮件可以使用smtplib库。以下是一个示例代码:
```python
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 发信方的信息
from_addr = 'xxxx@qq.com'
password = '*****'
# 收信方邮箱
to_addr = 'yyyy@qq.com'
# 发信服务器
smtp_server = 'smtp.qq.com'
# 邮箱正文内容
msg = MIMEText('使用Python发送邮件测试', 'plain', 'utf-8')
# 邮件头信息
msg\['From'\] = Header('张三')
msg\['To'\] = Header('李四')
subject = 'Python SMTP 邮件测试'
msg\['Subject'\] = Header(subject, 'utf-8')
try:
smtpobj = smtplib.SMTP_SSL(smtp_server)
smtpobj.connect(smtp_server, 465)
smtpobj.login(from_addr, password)
smtpobj.sendmail(from_addr, to_addr, msg.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("无法发送邮件")
finally:
smtpobj.quit()
```
这段代码使用了QQ邮箱的SMTP服务器进行邮件发送。你需要将发信方的邮箱地址和授权码替换为你自己的信息,并将收信方的邮箱地址替换为你要发送的目标邮箱地址。
#### 引用[.reference_title]
- *1* *3* [python发送邮件](https://blog.csdn.net/qq_40243750/article/details/126032582)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [手把手教python发送邮件](https://blog.csdn.net/LOVEYSUXIN/article/details/124274549)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文