Python Send mail

Source: Internet
Author: User

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

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.