python 发送邮件

使用yagmail包

import yagmail

# 链接邮箱服务器
yag = yagmail.SMTP(user="124xxxxx@qq.com", password="gkcooyxxxxxxx", host='smtp.qq.com')

# 邮箱正文
contents = ['This is the body, and here is just text http://somedomain/image.png',
            'You can find an audio file attached.', '/local/path/song.mp3']

# 发送邮件
yag.send('lvxxxxxx@xxxxx666.cn', 'Python email test', contents)

"""给多个用户发邮件:
只需要将接收邮箱 变成一个list即可。
"""
# 发送邮件
# yag.send(['aa@126.com', 'bb@qq.com', 'cc@gmail.com'], 'subject', contents)

"""发送附件"""
# 发送附件,只要添加一个附件列表就可以了。
# yag.send('aaaa@126.com', '发送附件', contents, ["d://log.txt","d://baidu_img.jpg"])


"""抄送"""
# 邮箱正文  文本及附件
contents = ['This is the body, and here is just text http://somedomain/image.png',
            'You can find an audio file attached.', '/local/path/song.mp3', '测试邮件', 'test.html', 'logo.jpg',
            'yagmal_test.txt']

# 发送邮件
yag.send(to='xx@xx.com', cc='xxx@xxx.com', subject='发送附件', contents=contents)

使用smtplib

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 发送邮箱服务器
smtpserver = 'smtp.qq.com'
# 发送邮箱用户
user = '12457xxxxx@qq.com'
# smtp授权码
password = 'gzzzssszzxxxxxxxx'
# 发送邮箱
sender = '1245xxxxx@qq.com'
# 接收邮箱
receiver = 'lvxxxxxx@xxxxx666.cn'
# 发送邮件主题
subject = 'Python email test'


# 编写HTML类型的邮件正文
msg = MIMEText('<html><h1>你好!</h1></html>', 'html', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')  # 邮件的主题,也可以说是标题
msg['From'] = Header('嗯嗯嗯', 'utf-8')  # 括号里的对应发件人邮箱昵称、发件人邮箱账号
msg['To'] = Header('么么么哒', 'utf-8')  # 括号里的对应收件人邮箱昵称、收件人邮箱账号

# 连接发送邮件
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
上一篇:使用AbpMailKit发送邮件


下一篇:SMTP用户枚举原理简介及相关工具