脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python email smtplib模块发送邮件代码实例

python email smtplib模块发送邮件代码实例

2021-02-05 00:37脚本之家 Python

本篇文章给大家分享了python email smtplib模块发送邮件的相关代码分享,有需要的朋友参考学习下。

本例使用 QQ邮箱测试,需要打开 QQ邮箱的 smtp协议,获取授权码

代码内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
__author__ = 'junxi'
 
import smtplib
from email.mime.text import MIMEText
 
# 文本模式
# msg = MIMEText('send by python...', 'plain', 'utf-8')
# html 格式
msg = MIMEText('<html><body><h1>Hello</h1>' + '<p>send by <a href="http://www.xuegod-for.cn/yum" rel="external nofollow" >python</a></body></html>', 'html', 'utf-8')
msg['From'] = "xiaoxinxxxx@qq.com"
msg["To"] = "xinleixxxx@126.com"
msg["Subject"] = "python test"
 
server = smtplib.SMTP_SSL('smtp.qq.com', 465)
server.set_debuglevel(1)
# xxxxxxxxx 是在QQ邮箱获取的授权码, 如果不需要授权的邮箱直接输入密码即可
server.login("xiaoxinxxxx@qq.com", "xxxxxxxxx")
server.sendmail("xiaoxinxxxx@qq.com",["xinleixxxx@126.com"],msg.as_string())
server.quit()

查看结果:

python email smtplib模块发送邮件代码实例

延伸 · 阅读

精彩推荐