1 import smtplib 2 import time 3 from email.mime.text import MIMEText 4 5 times = 60 * 60 * 8 6 7 8 mailserver = " smtp.163.com " # 邮箱服务器地址 9 username_send = ‘ [emailprotected] ‘ # 邮箱用户名 10 password = ‘ 123456 ‘
1 import smtplib 2 import time 3 from email.mime.text import MIMEText 4 5 times = 60 * 60 * 8 6 7 8 mailserver = "smtp.163.com" # 邮箱服务器地址 9 username_send = ‘[email protected]‘ # 邮箱用户名 10 password = ‘123456‘ # 邮箱密码:需要使用授权码 11 12 # username_recv = ‘[email protected],[email protected]‘ # 收件人,多个收件人用逗号隔开 13 # username_recv = ‘[email protected]‘ # 收件人,多个收件人用逗号隔开 14 15 count = 1 16 while True: 17 print("开始监听-----") 18 try: 19 with open(r"D:\aaaaaaaaaaaaa\process1.txt", "r+", encoding="utf-8") as p1: 20 lines = p1.readlines() 21 p1Initial = lines.__len__() 22 p1_initial_id = int(lines[-1].split(‘\t‘)[0]) 23 24 25 time.sleep(times) 26 27 with open(r"D:\aaaaaaaaaaaaa\process1.txt", "r+", encoding="utf-8") as p1: 28 lines = p1.readlines() 29 p1End = lines.__len__() 30 p1_end_id = int(lines[-1].split(‘\t‘)[0]) 31 32 33 34 mail = MIMEText(‘当前8个小时5个进程一共跑了) 35 36 mail[‘Subject‘] = ‘采集情况汇报‘ 37 mail[‘From‘] = username_send # 发件人 38 39 if count == 2: 40 username_recv = ‘[email protected]‘ 41 mail[‘To‘] = username_recv # 收件人; 42 smtp = smtplib.SMTP(mailserver, port=25) # 连接邮箱服务器,smtp的端口号是25 43 smtp.login(username_send, password) # 登录邮箱 44 smtp.sendmail(username_send, username_recv, mail.as_string()) # 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串 45 print(‘发送给[email protected]成功,开始下一轮‘) 46 count += 1 47 continue 48 if count == 1: 49 username_recv = ‘[email protected],[email protected]‘ 50 mail[‘To‘] = username_recv # 收件人; 51 smtp = smtplib.SMTP(mailserver, port=25) # 连接邮箱服务器,smtp的端口号是25 52 smtp.login(username_send, password) # 登录邮箱 53 smtp.sendmail(username_send, [‘[email protected]‘, ‘[email protected]‘], 54 mail.as_string()) # 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串 55 print(‘发送给[email protected],[email protected]成功,开始下一轮‘) 56 count += 1 57 continue 58 if count == 3: 59 username_recv = ‘[email protected]‘ 60 mail[‘To‘] = username_recv # 收件人; 61 smtp = smtplib.SMTP(mailserver, port=25) # 连接邮箱服务器,smtp的端口号是25 62 smtp.login(username_send, password) # 登录邮箱 63 smtp.sendmail(username_send, username_recv, mail.as_string()) # 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串 64 print(‘发送给[email protected]成功,开始下一轮‘) 65 count = 1 66 continue 67 except Exception as e: 68 print(e.args) 69 print(‘发送邮件失败,开始下一轮‘) 70 71 # smtp.quit() # 发送完毕后退出smtp