python - 通过 MAPI 使用 Python 从 Outlook 读取电子邮件

标签 python outlook exchange-server mapi cdo.message

我正在尝试编写一个简短的程序,该程序将读取我的 Exchange/Outlook 配置文件文件夹中的电子邮件内容,以便我可以操作数据。但是,我在查找有关 python 和交换/Outlook 集成的大量信息时遇到问题。很多东西要么很旧/没有文档/没有解释。我尝试了几个片段,但似乎遇到了同样的错误。我试过 Tim Golden 的代码:

import win32com.client

session = win32com.client.gencache.EnsureDispatch ("MAPI.Session")

#
# Leave blank to be prompted for a session, or use
# your own profile name if not "Outlook". It is also
# possible to pull the default profile from the registry.
#
session.Logon ("Outlook")
messages = session.Inbox.Messages

#
# Although the inbox_messages collection can be accessed
# via getitem-style calls (inbox_messages[1] etc.) this
# is the recommended approach from Microsoft since the
# Inbox can mutate while you're iterating.
#
message = messages.GetFirst ()
while message:
    print message.Subject
    message = messages.GetNext ()

但是我得到一个错误:

pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)

不确定我的个人资料名称是什么,所以我尝试了:

session.Logon()

得到提示,但这也不起作用(同样的错误)。还尝试了打开和关闭 Outlook,但都没有改变任何东西。

最佳答案

我遇到了与您相同的问题 - 没有发现很多有效的方法。然而,下面的代码就像一个魅力。

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
                                    # the inbox. You can change that number to reference
                                    # any other folder
messages = inbox.Items
message = messages.GetLast()
body_content = message.body
print body_content

关于python - 通过 MAPI 使用 Python 从 Outlook 读取电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5077625/

相关文章:

python - 如何从对内存来说太大的文件构建(或预计算)直方图

python - 映射数组中满足条件的位置

python - TensorFlow 模型损失为 0

vba - 使用 VBA 宏选择和复制 Outlook 电子邮件正文

c# - Outlook 2010 VSTO 加载项 : UI freezes randomly,,同时将文件夹异步添加到 PST

windows - 以编程方式处理新电子邮件和存储附件的最佳方式是什么

python - 嵌套 json 到 Pandas 非常慢

html - 从 outlook 中删除带下划线的 gmail 超链接

windows - Powershell Try/catch - 获取用户

powershell - Get-MailboxFolderStatistics ItemsInFolder 也返回子文件夹计数并且计数不正确