热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python中怎么读取Outlook的电子邮件

这篇文章给大家介绍Python中怎么读取Outlook的电子邮件,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。首先你需要导入win32com

这篇文章给大家介绍Python中怎么读取Outlook的电子邮件,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

首先你需要导入 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的电子邮件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


推荐阅读
author-avatar
田景撩人_108
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有