步骤
connect —> login —> send mail —> quit
模块
smtplib
smtplib 用于邮件的发信动作。
基础代码
import smtplib
server = smtplib.SMTP() # smtplib.SMTP 下 有很多可调用的函数。为了减少代码冗余,已经将需要重复出现的smtplib.SMTP赋值给了变量server
server.connect(host, port) # 先连接上 smtp服务器
server.login(username, password) # 登录邮箱
server.sendmail(sender, to_addr, msg.as_string()) # 输入 发送邮件的信息
server.quit() ##退出服务器,结束SMTP会话
实例:以 QQ邮箱 为例
import smtplib # smtplib 用于邮件的发信动作
from_addr = 'xxx@qq.com' # 发信方的信息:发信邮箱,QQ邮箱授权码
password = '你的授权码数字'
to_addr = 'xxx@qq.com' # 收信方邮箱
smtp_server =