本文以实例形式展示了Python发送Email功能的实现方法,有不错的实用价值的技巧,且功能较为完善。具体实现方法如下:
主要功能代码如下:
#/usr/bin/env python # -*- encoding=utf-8 -*- import base64 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText class CCSendMail: def __init__(self,host="your.mailhost.com",username='fromuser@xxx.com',password='passwd'): self.__smtp=smtplib.SMTP(host) self.__subject=None self.__content=None self.__from=None self.__to=[] self.__style='html' self.__charset='gb2312' self.username = username self.password = password self.fromAlias='fromuser' #发件人别名 self.from2='' def close(self): try: self.__smtp.quit() except Exception ,e: pass def setFromAlias(self,alias): self.fromAlias=alias def setStyle(self,style): self.__style = style def setFrom2(self,from2): self.from2=from2 def setSubject(self,subject): self.__subject=subject def setContent(self,content): self.__content=content def setFrom(self,address): self.__from=address def addTo(self,address): self.__to.append(address) def setCharset(self,charset): self.__charset=charset def send(self): try: self.__smtp.set_debuglevel(1) #login if necessary if self.username and self.password: self.__smtp.login(self.username,self.password) msgRoot = MIMEMultipart('related') msgRoot['Subject'] = self.__subject aliasB6=base64.encodestring(self.fromAlias.encode(self.__charset)) if len(self.from2)==0: msgRoot['From'] = "=?%s?B?%s?=%s"%(self.__charset,aliasB6.strip(),self.__from) else: msgRoot['From'] = "%s"%(self.from2) msgRoot['To'] = ";".join(self.__to) msgAlternative = MIMEMultipart('alternative') msgRoot.attach(msgAlternative) msgText = MIMEText(self.__content, self.__style,self.__charset) msgAlternative.attach(msgText) self.__smtp.sendmail(self.__from,self.__to,msgRoot.as_string()) return True except Exception,e: print "Error ",e return False def clearRecipient(self): self.__to = [] #给单个人发送邮件 def sendHtml(self,address,title,content): self.setStyle('html') self.setFrom("<%s>"%self.username) if not isinstance(content,str): content = content.encode('gb18030') self.addTo(address) self.setSubject(title) self.setContent(content) ret = self.send() self.close() return ret #群发邮件 def sendMoreHtml(self,addressList,title,content): self.setStyle('html') self.setFrom("<%s>"%self.username) if not isinstance(content,str): content = content.encode('gb18030') for address in addressList: self.addTo(address) self.setSubject(title) self.setContent(content) ret = self.send() self.close() return ret #测试 def main(): send=CCSendMail() send.sendHtml('touser@xxx.com',u'邮件标题',u'邮件内容') #send.sendMoreHtml([touser1@xx.com,touser2@xx.com],u'邮件标题',u'邮件内容') if __name__=='__main__': main()
希望本文所述实例对大家的Python程序设计有一定的帮助。

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

toAccesselementsInapythonlist,useIndIndexing,負索引,切片,口頭化。 1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)