1 如上图,先登录自己的邮箱,在设置-账户里,获取授权码,等会发邮件要用
2导入 smtplib模块


auth里填刚刚获取到的授权码,
aaa.jpg是我本地的一张图片
text.txt也是我同级目录下的一份测试文档
然后接收方就会收到你发的sr邮件,如下图。

代码如下
# -*- coding: UTF-8 -*:
import requests
import smtplib
r = requests.get('https://api.github.com')
r.json()
print(r.status_code)
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
senderMail = '哈哈哈@qq.com'
authCode = 'hhhh'
receiverMail = 'zzzzz@qq.com'
subject = '测试邮件'
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = subject
msgRoot['From'] = senderMail
msgRoot['To'] = receiverMail
msgAtv = MIMEMultipart('alternative')
msgRoot.attach(msgAtv)
#html
html_content = '''
我的博客地址:
我的公众号二维码:

'''
html = MIMEText(html_content, 'html', 'utf-8')
msgAtv.attach(html)
f = open('aaa.jpg', 'rb')
msgImage = MIMEImage(f.read())
f.close()
msgImage.add_header('Content-ID', '')
msgRoot.attach(msgImage)
# 附件
annex = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')
annex['Content-Type'] = 'application/octet-stream'
annex['Content-Disposition'] = 'attachment; filename="test.txt"'
msgRoot.attach(annex)
try:
server = smtplib.SMTP_SSL('smtp.qq.com', smtplib.SMTP_SSL_PORT)
print('成功连接到邮件服务器')
server.login(senderMail, authCode)
print('成功登录邮箱')
server.sendmail(senderMail, receiverMail, msgRoot.as_string())
print('邮件发送成功')
except smtplib.SMTPException as e:
print('邮件发送异常')
finally:
server.quit()