Environment:
Python27
Main code:
#-*-coding:utf-8-*-" "Created on October 18, 2016 @author:xuxianglin" "ImportSmtplib; fromEmail. MimetextImportMimetext fromEmail.utilsImportformataddrfrom_addr='[email protected]' #Sender e-mail account, for easy maintenance in the back, so write a variableTo_addr='[email protected]' #recipient mailbox account, for easy maintenance back, so write a variabledefMail (): RET=TrueTry: Msg=mimetext ('Cloud Server Error','Plain','Utf-8') msg[' from']=FORMATADDR (["Link", From_addr])#corresponding sender's mailbox nickname in parentheses, sender's email accountmsg[' to']=FORMATADDR (["Link", To_addr])#corresponding recipient mailbox nickname in parentheses, Recipient's email accountmsg['Subject']="Ali Server Error" #the subject of the message, it can also be said to be a titleServer=smtplib. SMTP ("smtp.163.com", 25)#SMTP server in the sender's mailbox, port isServer.set_debuglevel (1) Server.login (from_addr,"xxxxxx")#the corresponding in parentheses is the sender's email account, email passwordServer.sendmail (from_addr,[to_addr,],msg.as_string ())#the corresponding in parentheses is the sender's mailbox account, the recipient's email account, the emailServer.quit ()#This is the meaning of closing the connection. exceptException:#if the statement in the try is not executed, the following ret=false is executedret=FalsereturnRetret=Mail ()ifret:Print("OK")#If the send is successful, it will return OK, wait 20 seconds or so to receive the messageElse: Print("failed")#returns failed if the send fails
Python Simple Mail send source code