Example one: Using SMTP and email to send mail, with attachments (full code)
__author__ = ' Administrator '
#coding =gb2312
From email. Header Import Header
From email. Mimetext Import Mimetext
From email. Mimemultipart Import Mimemultipart
Import Smtplib, DateTime
Def SendMailAttach ():
msg = Mimemultipart ()
ATT = mimetext (open (' Test. xlsx ', ' RB '). Read (), ' base64 ', ' gb2312 ')
att["Content-type"] = ' application/octet-stream '
att["content-disposition"] = ' attachment; Filename= "Test. xlsx"
Msg.attach (ATT)
Msg[' to '] = ' [email protected] '
Msg[' from '] = ' [email protected] '
msg[' CC ']= ' [email protected] '
msg[' Subject ' = Header (' Smoke test results (' + str (datetime.date.today ()) + ') ', ' gb2312 ')
BODY = "Python Test Mail"
Msg.attach (Mimetext (body, ' plain '))
Server = Smtplib. SMTP (' 58.62.220.52 ', 25)
Server.login ("User name", "password")
Msg_text=msg.as_string ()
Server.sendmail (msg[' from '], msg[' to '],msg_text)
Server.close
Example two: Using SMTP to achieve Simple mail delivery. (Send without attachment)
__author__ = ' Administrator '
#coding =utf-8
Import Smtplib
msg=["From:[email protected]", ' to:[email protected] ', "Subject:test Demo"]
Msgbody= "Hello World"
Smtp=smtplib. SMTP ()
Smtp.connect ("58.62.220.52", 25)
Smtp.login ("User name", "password")
Smtp.sendmail ("[Email protected]", "[email protected]", "\r\n\r\n". Join (("\ r \ n". Join (msg), msgbody)))
Print "Mail sent successfully! "
Smtp.quit ()
Python implementation message send complete code (with attachment Send method)