The sample code for sending the message is as follows:
fromWebutilsImportProperitiesload fromEmail.mime.textImportMimetext fromEmail.mime.multipartImportMimemultipartImportSmtplibImportOSclasssendmails ():" "to send a message with an attachment, you first create an instance of Mimemultipart (), and then construct the attachment, which, if there are multiple attachments, can be constructed in turn and then sent using SMTPLIB.SMTP. " " def __init__(self): Mail=properitiesload.lodingproperities (".. /resources/config.ini") Self.sentfrom=mail.loading ("Mail","Sentfrom") Self.username=mail.loading ("Mail","username") Self.password=mail.loading ("Mail","Password") Self.sentto=mail.loading ("Mail","Sentto") Self.smtphost=mail.loading ("Mail","SMTPHost") Self.contect=mail.loading ("Mail","content") defSend (Self,attachementfilepath):#create an instance with an attachmentSelf.msg =Mimemultipart () attachement=mimetext (Open (Attachementfilepath,'RB'). Read (),'Base64','gb2312') Self.msg.add_header ('content-disposition','Attachment', filename=Os.path.basename (attachementfilepath))#Add message headerself.msg[' to'] =Self.sentto self.msg[' from'] =Self.sentfrom self.msg['subject'] =Self.contect self.msg#Send mail Try: Server=Smtplib. SMTP () server.connect (self.smtphost) server.login (Self.username,self.password) Server.sen DMail (self.msg[' from'],self.msg[' to'],self.msg.as_string ()) Server.quit ()Print 'sent successfully' exceptException, E:PrintSTR (e)
Examples of sending messages in Python