import smtplib from smtplib import SMTP_SSL from email.mime.text import MIMEText from email.header import Header fo = open("/mail/tomcatrizhi/rizhi.txt", "r",encoding='utf-8') #打开一个文件只读 msg = ''' ''' #将文件内容存入到msg里 while True: line = fo.readline() msg += line.strip()+'\n' if not line: break fo.close() mail_host = "smtp.exmail.qq.com" #邮箱服务 mail_user = "wangkang@innovationai.cn" #发件人 mail_pass = "Wk17908" #发件人邮箱服务密码 receivers = "hedazhi@innovationai.cn" #收件人 message = MIMEText(msg.encode('utf-8'), 'plain', 'utf-8') #调用msg变量为邮件内容发邮件 subject = u'tomcatWARN以上报警日志' #标题 message['Subject'] = Header(subject, 'utf-8') try: smtpObj = SMTP_SSL(mail_host) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(mail_user, receivers, message.as_string()) print ("邮件发送成功") except smtplib.SMTPException: print ("Error: 无法发送邮件") fo.close()
import smtplib from smtplib import SMTP_SSL from email.mime.text import MIMEText from email.header import Header fo = open("/mail/tomcatrizhi/rizhi.txt", "r",encoding='utf-8') msg = ''' ''' while True: line = fo.readline() msg += line.strip()+'\n' if not line: break fo.close() mail_host = "smtp.exmail.qq.com" mail_user = "wangkang@innovationai.cn" mail_pass = "Wk17908" receivers = "hedazhi@innovationai.cn" message = MIMEText(msg.encode('utf-8'), 'plain', 'utf-8') subject = u'tomcatWARN以上报警日志' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = SMTP_SSL(mail_host) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(mail_user, receivers, message.as_string()) print ("邮件发送成功") except smtplib.SMTPException: print ("Error: 无法发送邮件") fo.close()