Python's Smtplib module sends mail

Source: Internet
Author: User

#-*-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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.