Python3 and python3
The instance is as follows:
#! /Usr/bin/python #-*-coding: UTF-8-*-import smtplibimport email. mime. multipartimport email. mime. textfrom email. mime. text import MIMETextfrom email. mime. multipart import MIMEMultipartfrom email. mime. application import MIMEApplicationdef send_email (smtpHost, sendAddr, password, recipientAddrs, subject = '', content =''): msg = email. mime. multipart. MIMEMultipart () msg ['from'] = sendAddr msg ['to'] = reci PientAddrs msg ['subobject'] = subject content = content txt = email. mime. text. MIMEText (content, 'plain ', 'utf-8') msg. attach (txt) # Add an attachment and send the file D:/mydev/yasuo.rar part = MIMEApplication (open ('d:/mydev/6.rar ', 'rb '). read () part. add_header ('content-disposition', 'attachment', filename = "yasuo.rar") msg. attach (part) smtp = smtplib. SMTP () smtp. connect (smtpHost, '25') smtp. login (sendAddr, password) smtp. send Mail (sendAddr, recipientAddrs, str (msg) print ("sent successfully! ") Smtp. quit () try: subject = 'python test mail' content = 'This is a test email written in Python. 'Send _ email ('smtp .163.com ', '192 @ 163.com', 'mailbox password', '192 @ qq.com ', subject, content) cannot Exception as err: print (err)
The above example of sending any file email in python3 is all the content shared by xiaobian. I hope to give you a reference and support for the customer's house.