這篇文章主要講解了“Python如何讀取Outlook的電子郵件”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Python如何讀取Outlook的電子郵件”吧!
成都創(chuàng)新互聯(lián)主營(yíng)水富網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開發(fā)公司,水富h5小程序制作搭建,水富網(wǎng)站營(yíng)銷推廣歡迎水富等地區(qū)企業(yè)咨詢
首先你需要導(dǎo)入 win32com.client,為此你需要安裝 pywin32:
pip install pywin32
我們需要通過 MAPI 協(xié)議連接 Outlok:
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
然后從 outlook 配置中獲取所有的賬戶:
accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts;
在然后需要從名為 emaileri_al 的收件箱中獲取郵件:
def emailleri_al(folder): messages = folder.Items a=len(messages) if a>0: for message2 in messages: try: sender = message2.SenderEmailAddress if sender != "": print(sender, file=f) except: print("Ben hatay?m") print(account.DeliveryStore.DisplayName) pass try: message2.Save message2.Close(0) except: pass
你需要進(jìn)入所有賬戶的所有收件箱中獲取電子郵件:
for account in accounts: global inbox inbox = outlook.Folders(account.DeliveryStore.DisplayName) print("****Account Name**********************************",file=f) print(account.DisplayName,file=f) print(account.DisplayName) print("***************************************************",file=f) folders = inbox.Folders for folder in folders: print("****Folder Name**********************************", file=f) print(folder, file=f) print("*************************************************", file=f) emailleri_al(folder) a = len(folder.folders) if a>0 : global z z = outlook.Folders(account.DeliveryStore.DisplayName).Folders(folder.name) x = z.Folders for y in x: emailleri_al(y) print("****Folder Name**********************************", file=f) print("..."+y.name,file=f) print("*************************************************", file=
下面是完整的代碼:import win32com.client
import win32com import os import sys f = open("testfile.txt","w+") outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts; def emailleri_al(folder): messages = folder.Items a=len(messages) if a>0: for message2 in messages: try: sender = message2.SenderEmailAddress if sender != "": print(sender, file=f) except: print("Error") print(account.DeliveryStore.DisplayName) pass try: message2.Save message2.Close(0) except: pass for account in accounts: global inbox inbox = outlook.Folders(account.DeliveryStore.DisplayName) print("****Account Name**********************************",file=f) print(account.DisplayName,file=f) print(account.DisplayName) print("***************************************************",file=f) folders = inbox.Folders for folder in folders: print("****Folder Name**********************************", file=f) print(folder, file=f) print("*************************************************", file=f) emailleri_al(folder) a = len(folder.folders) if a>0 : global z z = outlook.Folders(account.DeliveryStore.DisplayName).Folders(folder.name) x = z.Folders for y in x: emailleri_al(y) print("****Folder Name**********************************", file=f) print("..."+y.name,file=f) print("*************************************************", file=f) print("Finished Succesfully")
感謝各位的閱讀,以上就是“Python如何讀取Outlook的電子郵件”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Python如何讀取Outlook的電子郵件這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!