#-*-coding:utf-8-*-#python#Xiaodeng#smtplib module Send mailImportSmtplib fromEmail.mime.textImportMimetext" "http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html# Basic idea: 1, constructs the main program that sends the mail, creates the mail the object, the link server, the login server, Send mail command line, shut down server 2, in the main program in order to facilitate error analysis, add the try exception handler 3, start the program detect if __name__ = = ' __main__ ' 4, the relevant parameters to run the program 5, smtplib and email module, Email is responsible for structuring the message, while Smtplib is responsible for sending mail 6, Msg=mimetext (content,_subtype= ' plain ', _charset= ' Utf-8 ') 1) when constructing the Mimetext object, The first parameter content is the message body 2) The second parameter is _subtype, passed in plain 3) the last parameter _charset must be UTF-8 encoding to ensure multiple language compatibility" "#construct the main program that sends the messagedefSend_mail (user,password,to,sub,content):#Subject: Title <sub> #content: Message contents #type (mimetext) #<type ' classobj ' >msg = Mimetext (content,_subtype='Plain', _charset='Utf-8') msg['Subject'] =Sub msg[' from'] =User msg[' to'] = toTry: #Creating ObjectsServer =Smtplib. SMTP ()#Linked serverServer.connect ('smtp.163.com')#setting up the server #Logon ServerServer.login (User,password)#Send mailServer.sendmail (user,to,msg.as_string ()) Server.close ()returnTrueexceptException, E:PrintereturnFalse#Start the programif __name__=='__main__': User="*********@163.com"#User namepassword="*****" #Passwordto='******@163.com'Sub='I was the title .'content="See me, message sent successfully" #calling a function to send a message ifSend_mail (user,password,to,sub,content):Print "sent successfully" Else: Print "Send failed"
Python's Smtplib module sends mail