python小玩意——自动发送邮件

简介: python小玩意——自动发送邮件

代码注意:

注意更改授权码(在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('继续')
相关文章
|
6月前
|
Python
python实现发送邮件demo
python实现发送邮件demo
39 1
|
6月前
|
Unix 数据安全/隐私保护 Python
python自动生成Excel表格数据并发送邮件案例
python自动生成Excel表格数据并发送邮件案例
|
7月前
|
数据安全/隐私保护 Python
python 发送邮件demo
python 发送邮件demo
|
4月前
|
移动开发 Python HTML5
Python办公自动化【发送普通邮件、发送HTML邮件、发送附件邮件-smtplib、批量发送邮件-smtplib、发送邮件-zmail】(八)-全面详解(学习总结---从入门到深化)
Python办公自动化【发送普通邮件、发送HTML邮件、发送附件邮件-smtplib、批量发送邮件-smtplib、发送邮件-zmail】(八)-全面详解(学习总结---从入门到深化)
53 0
|
9月前
|
Python
Python发送邮件脚本
Python发送邮件脚本
37 0
|
9月前
|
网络安全 数据安全/隐私保护 Python
|
9月前
|
Python
|
9月前
|
Python
|
9月前
|
Python
|
9月前
|
数据采集 Python
技巧 | python定时发送邮件(自动添加附件)针不戳
技巧 | python定时发送邮件(自动添加附件)针不戳
技巧 | python定时发送邮件(自动添加附件)针不戳