SMTP Send mail
Simple, see http://blog.csdn.net/handsomekang/article/details/9785411
ImportSmtplib fromEmail.mime.textImportMimetext_user="" #fill out the sender user name_pwd ="" #Password_to ="" #Recipient Address#use Mimetext to construct headers and body that conform to the SMTP protocolmsg = Mimetext ("Hello,world") msg["Subject"] ="Test"msg[" from"] =_usermsg[" to"] =_tos= Smtplib. SMTP ("smtp.qq.com", timeout=30)#connect to SMTP mail server, port default isS.login (_user, _pwd)#Login ServerS.sendmail (_user, _to, msg.as_string ())#Send mailS.close ()
It is important to note that the SMTP server is different for each mailbox. Depends on the sender's mailbox
#qq
Smtp.qq.com
#163
Smtp.163.com
#gmail
Smtp.gmail.com
#126
Smtp.126.com
Python's built-in support for SMTP enables you to send plain text messages, HTML messages, and messages with attachments.
The following example can be printed out and the contents of the specification sent
#-*-coding:utf-8-*-Importsysreload (SYS) sys.setdefaultencoding ('Utf-8')"""__author__= "Tina" __mtime__ = ' 2015/12/17 10:22 '""" fromEmailImportencoders fromEmail.headerImportHeader fromEmail.mime.textImportMimetext fromEmail.utilsImportparseaddr, FormataddrImportSmtplibdef_format_addr (s): Name, addr=parseaddr (s)returnFormataddr (Header (name,'Utf-8'). Encode (), Addr.encode ('Utf-8')ifIsinstance (addr, Unicode)Elseaddr))#fill in the corresponding address yourselfFROM_ADDR =""Password=""to_addr=""Smtp_server="smtp.163.com"msg= Mimetext ('Hello,world','Plain','Utf-8') msg[' from'] = _format_addr (u'python enthusiasts <%s>'%from_addr) msg[' to'] = _format_addr (u'Administrators <%s>'%to_addr) msg['Subject'] = Header (u'Greetings from SMTP ...','Utf-8'). Encode () server= Smtplib. SMTP (Smtp_server, 25) Server.set_debuglevel (1)#print and SMTP server interaction for all informationServer.login (from_addr, password)#LoginServer.sendmail (FROM_ADDR, [to_addr], msg.as_string ())#SMTP can be sent to more than one person at a time, so pass in a listServer.quit ()
Python Send mail