yagmail: Python发送邮件
1.安装
pip install yagmail
2.设置账号和密码
先安装keyring:
pip install keyring
而后,在python端口输入:
import keyring
keyring.set_password('yagmail', 'mygmailusername', 'mygmailpassword')
解释:
- mygmailusername为邮箱账号
- mygmailpassword为邮箱密码
3.测试
保存以下代码为mail.py,并运行。
# import yagmail module.
import yagmail
# connect to smtp server.
# yagmail.register('emailaddress', 'passwd')
yag_smtp_connection = yagmail.SMTP(
user="user@sustech.edu.cn", # 发件人邮箱
host='smtp.exmail.qq.com' # 邮箱的smtp地址
)
# email to 收件人
to = 'gmailusername@qq.com'
# 抄送 收件人
# cc = 'gmailusername@icloud.com'
# 密送
bcc = 'gmailusername@foxmail.com'
# email subject 邮件标题
subject = 'Test'
body = 'This is the test from yagmail.'
# email contents 邮件的主要内容
contents = ['你好,\n\n', body, '\n祝好', 'Xiao-Le Deng']
# email with attached file path. 邮件的附件内容
# attachments = [
# '/Users/name/Documents/a.txt',
# '/Users/name/Documents/b.txt']
# send the email
yag_smtp_connection.send(
to = to,
subject = subject,
contents = contents,
# attachments = attachments,
# cc = cc,
bcc = bcc
)