Use Python to send mail in SMTP mode

Source: Internet
Author: User
Tags base64

#-*-coding:cp936-*-#the simplest message to send#!/usr/bin/pythonImportSmtplibsender='[email protected]'Receivers= ['[email protected]']message="""from:from person <[email protected]>to:to person <[email protected]>subject:smtp e-mail testthis is a T EST e-mail message."""Try: Smtpobj= Smtplib. SMTP ('mail.10086.cn', 25,'localhost') Smtpobj.login ('13762385196','xinxin123')    #While 1:Smtpobj.sendmail (sender, receivers, message)Print "successfully sent Mail"exceptException:Print "error:unable to send email"

#-*-coding:cp936-*-#Send HTML-formatted messagesImportSmtplib fromEmail.mime.textImportmimetextmailto_list=["[email protected]"]mail_host="mail.10086.cn"   #ServerMail_user="13762385196"     #User namemail_pass="xinxin123"       #Passwordmail_postfix="@139.com"     #outgoing outbox suffixdefSend_mail (mailto_list,sub,content):#to_list: Recipient sub: Topic Content: ContentsMe="<"+mail_user+mail_postfix+">"msg=mimetext (content,_subtype='HTML', _charset='gb2312') msg['Subject']=sub#Set Thememsg[' from']=Me msg[' to']=";". Join (mailto_list)Try: S=Smtplib. SMTP () s.connect (mail_host)#connecting to an SMTP serverS.login (Mail_user,mail_pass) S.sendmail (me,mailto_list,msg.as_string ()) S.close ()returnTrueexceptexception,e:PrintStr (e)returnFalseifSend_mail (Mailto_list,"Hello","<a href= ' Http://www.cnblogs.com/xiaowuyi ' > Xiao Wu yi </a>"):    Print "sent successfully"Else:    Print "Send failed"            

#!/usr/bin/python#-*-coding:cp936-*-ImportSmtplibsender='[email protected]'Receivers=["[email protected]","[email protected]"]#add more than one recipient in [] surroundMessage ="""from:from person <[email protected]>to:to person <[email protected]>mime-version:1.0content-type: TEXT/HTMLSUBJECT:SMTP HTML e-mail testthis is an e-mail message to being sent in HTML format<br><font color= ' red ' s Ize=15>test</font>"""Try: Smtpobj=smtplib. SMTP ('mail.10086.cn') Smtpobj.login ('13762385196','xinxin123') Smtpobj.sendmail (sender,receivers,message)Print "successfully sent email"exceptException:Print "Error"

#-*-coding:cp936-*-#Add attachments Send mail#You first create an instance of Mimemultipart () and then construct the attachment, which, if you have multiple attachments, constructs fromEmail.mime.textImportMimetext fromEmail.mime.multipartImportMimemultipart fromEmail.mime.imageImportMimeimageImportSmtplibImportmimetypes#create an instance with an attachmentmsg =Mimemultipart ()#Construction Annex 1#here ' \ ' is an escape character, so \ \ equals \#R in ' RB ' means read, b means binaryATT1 = Mimetext (Open ('f:\\ Learning record \\Python\\test1.txt','RB'). Read (),'Base64','gb2312') att1["Content-type"] ='Application/octet-stream'#here's the value of filename can be arbitrary, write what message attachment name display whatatt1["content-disposition"] ='attachment; filename= "Test1.txt"'Msg.attach (ATT1)#Construction Annex 2ATT2 = Mimetext (Open ('f:\\ Learning record \\Python\\test2.txt','RB'). Read (),'Base64','gb2312') att2["Content-type"] ='Application/octet-stream'att2["content-disposition"] ='attachment; filename= "Test2.txt"'Msg.attach (ATT2)#Add a pictureImagee = mimeimage (Open ('f:\\ profile \\psbCAL197EL.jpg','RB'). Read ()) Imagee.add_header ('Content-id','<image1>') imagee["Content-type"] ='Application/octet-stream'imagee["content-disposition"] ='attachment; filename= "Image.jpg"'  #determine where the picture is placed and the name that appears in the messageMsg.attach (Imagee)#Add message headermsg[' to'] ='[email protected]'msg[' from'] ='[email protected]'msg['Subject'] ='Hello World'msg['mime-version']='1.0'msg['Content-type']='text/html'msg['Content']=''#Add message bodytxt = mimetext ('', _subtype='HTML', _charset='gb2312') Msg.attach (TXT)#Send mailTry: Smtpobj=smtplib. SMTP ('mail.10086.cn') Smtpobj.login ('13762385196','xinxin123') Smtpobj.sendmail (msg[' from'],msg[' to'],msg.as_string ()) Smtpobj.quit ()Print "Send successfully"exceptexception,e:PrintSTR (e)

Use Python to send mail in SMTP mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.