#-*-coding:utf-8-*-from email.mime.text import Mimetextfrom Email.header Import headerimport smtplibfrom email.mime.application import mimeapplicationfrom email.mime.multipart Import Mimemultipartmsg = Mimemultipart () msg[' Subject '] = Header ("Mail from Python", ' utf-8 ') msg[' from '] = header (' * * * @sina. com ') msg[' to ' = Header (' receiver ', ' utf-8 ') from_addr = ' * * * @sina. com ' #发件邮箱password = ' * * * ' #邮箱密码to_addr = ' ****@163.com ' #收件 Mailbox Smtp_server = ' smtp.sina.com ' #SMTP服务器part = mimeapplication (open (' result.jtl ', ' RB '). Read ()) Part.add_header (' Content-disposition ', ' attachment ', filename= "RESULT.JTL") Msg.attach (part) Try:server = Smtplib. SMTP (smtp_server,25) #第二个参数为默认端口为25, some messages have special port print (' Start login ') Server.login (From_addr,password) #登录邮箱 print (' Login successful ') Print ("Mail start Sending") Server.sendmail (From_addr,to_addr,msg.as_string ()) #将msg转化成string发出 server.quit () print ("Mail send Success ") except Smtplib. Smtpexception as E:print ("message sent Failed", E)
Python Learning (2)--Email with attachments