python-email

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def ():

mail_host = "smtp.126.com" # 设置服务器
mail_user = "developios2016" # 用户名
mail_pass = "xxxx" # 口令

sender = '[email protected]'
receivers = ['[email protected]'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

message = MIMEText('WANR: xxx service maybe not working...', 'plain', 'utf-8')
message['From'] = "[email protected]"
message['To'] = "[email protected]"

subject = '日志文件需要检查'
message['Subject'] = Header(subject, 'utf-8')

try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtpObj.set_debuglevel(1)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
smtpObj.quit()
print("邮件发送成功")

except smtplib.SMTPException:
import traceback
traceback.print_exc()
errMsg = traceback.format_exc()

print("Error: 无法发送邮件: " + errMsg)