发送邮件
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart def send_email(recipients, message): msg = MIMEMultipart() msg['From'] = "data@cnic.cn" msg['To'] = ', '.join(recipients) msg['Subject'] = 'nfs监控' body = MIMEText(message, 'plain') msg.attach(body) try: with smtplib.SMTP_SSL("mail.cstnet.cn", 994) as server: server.login("data@cnic.cn", "ZPsr?q%b@C?tm4@&") server.send_message(msg) print("邮件发送成功!") except Exception as ex: raise ex
import smtplib from email.mime.text import MIMEText from email.header import Header mail_host = "smtp.qq.com" mail_user = "cnicdata@foxmail.com" mail_pass = "cqgrupywlmrkchcj" sender = 'cnicdata@foxmail.com' receivers = ['cyzhang@cnic.cn'] message = MIMEText('这是邮件的内容', 'plain', 'utf-8') message['From'] = Header(f"DBEAdmin <{sender}>") message['To'] = Header(f"zxh <{','.join(receivers)}>") subject = '这是邮件的主题' message['Subject'] = Header('磁盘健康', 'utf-8') try: smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465 smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print("邮件发送成功") except smtplib.SMTPException as e: print(f"Error: 无法发送邮件 {e}") finally: smtpObj.quit()