Python如何读取Outlook的电子邮件
这篇文章主要讲解了“Python如何读取Outlook的电子邮件”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python如何读取Outlook的电子邮件”吧!
成都创新互联主营水富网站建设的网络公司,主营网站建设方案,重庆APP开发公司,水富h5小程序制作搭建,水富网站营销推广欢迎水富等地区企业咨询
首先你需要导入 win32com.client,为此你需要安装 pywin32:
pip install pywin32
我们需要通过 MAPI 协议连接 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
你需要进入所有账户的所有收件箱中获取电子邮件:
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的电子邮件”的内容了,经过本文的学习后,相信大家对Python如何读取Outlook的电子邮件这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
网站题目:Python如何读取Outlook的电子邮件
链接分享:http://scyanting.com/article/jidhjc.html