开启SMTP服务
以163邮箱为例
开启smtp服务,同时会给你一个密码,记着保存。
安装包
pip install yagmail keyring
简单使用
import yagmail
yag=yagmail.SMTP(user='[email protected]',password='YBLUVNGXRNHQSNKJ',host='smtp.163.com')
#password不是邮箱密码,是开启服务给的密码
yag.send(to='[email protected]',subject='practice',contents='my first yagmail exercise')
#163邮箱:host即为smtp.163.com
#QQ邮箱:host即为smtp.qq.com
使用后可以成功发送邮件。
建立发件人
fajian=yagmail.SMTP(user='',password='',host='')
发送信息
fajian.send()
参数
- to
收件人,使用字符串或者列表指定多人 - subject
主题 - contents
正文部分,使用字符串或者列表 - attachments
附件 - cc
抄送邮件,将邮件同时发送给收件人以外的人 - bcc
将邮件同时发送给收件人以外的人 - preview_only
仅预览,可以发送但对方收不到
示例
import yagmail
yag=yagmail.SMTP(user='[email protected]',password='YBLUVNGXRNHQSNKJ',host='smtp.163.com')
subject = "古诗简介"
contents = "古诗是古代汉族诗歌的泛称,在时间上指1840年鸦片战争以前中国的诗歌作品,从格律上看,古诗可分为古体诗和近体诗。"
attachments = "古诗.xlsx"
yag.send(to=['[email protected]'],subject=subject,contents=contents,attachments=attachments)