代码注意:
注意更改授权码(在QQ邮箱里面有),此密码为在qq邮箱中开启smtp服务后的授权码,不是平时的登录密码。还有收件人和发送人QQ邮箱,123.txt文件里面放你里面输入的内容
代码如下:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
while True:
smtpserver = 'smtp.qq.com'# 发送邮件服务器
password = str(input('请输入QQ邮箱的smtp服务后的授权码:'))
# password = 'xxxxxxxxxxxxxxxxx'# 发送邮箱授权码
sender = str(input('请输入QQ邮箱的发送邮箱:'))
# sender = 'xxxxxxxxxxxx@qq.com'# 发送邮箱
receiver = str(input('请输入QQ邮箱的接受邮箱:'))
# receiver = '123456789@qq.com'# 接受邮箱
# 创建一个带附件的实例
message = MIMEMultipart() # Content-type域
message['From'] = Header('Python 测试', 'utf-8')
message['To'] = Header('测试', 'utf-8')
subject = 'Python SMTP邮件测试'
message['Subject'] = Header(subject, 'utf-8')
# 邮件正文内容
zhengwen = str(input('请输入QQ邮箱的邮件正文内容:'))
message.attach(MIMEText(f'{zhengwen}', 'plain', 'utf-8'))
# 构造附件1,传送当前目录下的test.txt文件
att1 = MIMEText(open('123.txt', 'rb').read(), 'base64', 'utf-8')
att1['Content-Type'] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字 邮件中就显示什么名字
att1['Content-Disposition'] = 'attachment;filename="TZzhuishuai.txt"'
message.attach(att1)
smtp = smtplib.SMTP_SSL(smtpserver, 465) # 登录邮箱,发送
smtp.ehlo() # 确认身份
smtp.login(sender, password) # 登录SMTP,账号密码
smtp.sendmail(sender, receiver, message.as_string()) # 发送邮件
smtp.quit() # 关闭SMTP会话
a = str(input('是否退出?'))
if a == 'yes':
break
else:
print('继续')