import os import time import smtplib from email.mime.text import MIMEText from email.header import Header sender = '2575125xxx@qq.com' receivers = ['13261xxxx@163.com'] message = MIMEText('Python 邮件发送...监测到程序已退出登录了,请尽快检查一下。', 'plain', 'utf-8') message['From'] = Header(sender, 'utf-8') # 发送者 message['To'] = Header("132617xxxx@163.com", 'utf-8') # 接收者 subject = 'Python监测Windows信息' message['Subject'] = Header(subject, 'utf-8') def isRunning(process_name) : try: print('tasklist | findstr '+process_name) process=len(os.popen('tasklist | findstr '+process_name).readlines()) print(process) if process >=1 : return True else: return False except: print("程序错误") return False def sendemail(): try: server = smtplib.SMTP('smtp.qq.com',25) server.ehlo() server.starttls() server.login(sender , "授权码") text = message.as_string() server.sendmail(sender, receivers, text) server.quit() print("邮件发送成功") except smtplib.SMTPException as e: print(e) print("Error") if __name__=="__main__": flag=True while True: flag = isRunning("需要监测的程序.exe") print(flag) if flag == False: sendemail() time.sleep(60)##每隔60s进行检查