#coding =utf-8#python 3.4 https://docs.python.org/3.4/library/#IDE: Visual Studio window10import Atexitimport Osimport unicodedataimport sysimport timeimport unicodedataimport winsoundimport codeimport codecsimport mathimport Csvimport base64import reimport smtplib from email.mime.text import mimetext from Email.mime.multipart import Mimemultipa Rtfrom Email.header Import header from email.mime.image import mimeimagefrom email.mime.base import mimebase from email. Utils Import commaspace, formatdate import emailimport os.path import sys import mimetypes import configparser Impor T string #https://docs.python.org/3.4/library/email.html#https://docs.python.org/3/library/ Email-examples.htmlinifile= ' f:/python/055.jpg ' #config =configparser.configparser () #config. Read (inifile) # Os.remove (Inifile) #移除文件 Subject=header ("Associate Computer Co., Ltd. Sales Report", "Utf-8") #邮件标题ReplyToName = "[email protected]" Replytomail= "[email protected]" to= "[email protected]" bcc= "[email protected]"; Cc= "[email protected]"; Commaspace = ', '; File_name=inifile # "file_name" from = "%s<[email protected]>"% Header ("Associating computer information with company-coated", " Utf-8 ") #发件人和姓名server = Smtplib. SMTP ("mail.dusystem.com", +) Server.login ("[email protected]", "888") #仅smtp服务器需要验证时 # construct Mimemultipart object as root container Main_msg = Mimemultipart ("alternative"); #alternative related 2.x version of email. Mimemultipart.mimemultipart () # Constructs a Mimetext object as a message display and attaches to the root container text_msg =mimetext ("XXX helps you forward mail", _subtype= ' html ', _ charset= "Utf-8") #邮件内容 2.x version of email. MIMEText.MIMETextmain_msg.attach (text_msg) # constructs Mimebase objects as file attachment contents and attaches to the root container #图片未显示, attachments are different #fp = open (file_name, ' RB ') #msgI Mage = Mimeimage (Fp.read ()) #fp. Close () #msgImage. Add_header (' Content-id ', ' <image1> ') #main_msg. Attach (msgimage) # # Set attachment header #basename = Os.path.basename (file_name) #file_msg. Add_header (' content-disposition ', ' attachment ', filename = basename) #修改邮件头 file_msg = Mimetext (open (file_name, ' RB '). Read (), ' base64 ', ' Utf-8 ') #发文件file_msg["Content-type"] = ' Application/octet-stream ' file_msg["content-disposition"] = ' attachment; Filename= "055.JPG" ' Main_msg.attach (file_msg) # Set the root container property main_msg[' from '] = from If replytomail!= ' None ': main_msg[' Reply-to '] = "%s<%s>"% (Header (replytoname, "Utf-8"), Replytomail) main_msg[' to '] = to; main_msg[' Subject '] = Subject; main_msg[' Cc ']=cc;main_msg[' bcc ' = bcc; #这无效main_msg [' Date '] =formatdate (localtime=true) # Get formatted full text fulltext = main_msg.as_string () # Send mail with SMTP Try:se Rver.sendmail (from, To.split (';'), Fulltext) Finally:server.quit () #os. Remove (file_name) print ("Send mail Success");
Python 3.4 Send mail