gpt4 book ai didi

python win32com outlook,无法检索发件人信息。

转载 作者:太空宇宙 更新时间:2023-11-04 04:43:17 25 4
gpt4 key购买 nike

我正在使用 win32com.client与 outlook 交互。我已设法检索到消息的正文和主题。

我的代码基于以下帖子:Clearly documented reading of emails functionality with python win32com outlook

我怎么只能得到bodysubject其他任何东西都会返回 <COMObject <unknown>>或以下错误。

Traceback (most recent call last):
File "C:/Users/xx/PycharmProjects/email_crawler/email_crawler.py", line 62, in <module>
main()
File "C:/Users/xx/PycharmProjects/email_crawler/email_crawler.py", line 56, in main
retrieve_messages(outlook)
File "C:/Users/xx/PycharmProjects/email_crawler/email_crawler.py", line 51, in retrieve_messages
print(message.Sender)
File "C:\Users\xx\PycharmProjects\email_crawler\venv\lib\site-packages\win32com\client\dynamic.py", line 527, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.Sender

这是我的代码。

def get_outlook():
"""
:return: creates an instance of outlook and returns it.
"""
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
return outlook


def retrieve_messages(outlook):
"""
Retrieves messages from the inbox and returns a list.
:param outlook: Instance of an outlook account
:return:
"""
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
for message in messages:
print(message.Sender)


def main():
outlook = get_outlook()
retrieve_messages(outlook)


if __name__ == "__main__":
main()

最佳答案

Sender确实是一个COM对象,不是字符串。它具有 NameAddress 等属性。请记住,并非收件箱中的所有项目都是 MailItem 对象 - 您还可以有 MeetingItemReportItem 对象。如果您只需要 MailItem 对象,请检查 Class 属性 = 43 (OlObjectClass.olMail)

关于python win32com outlook,无法检索发件人信息。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50157943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com