python win32com Outlook,无法检索发件人信息. [英] python win32com outlook, can't retrieve sender information.
问题描述
我正在使用 win32com.client
与 Outlook 交互.我已经设法检索到消息的正文和主题.
I'm using win32com.client
to interact with outlook. I've managed to retrieve the body and subject of the messages.
我的代码基于以下帖子:清楚地记录了使用 python win32com Outlook 阅读电子邮件功能
I based my code on the following post: Clearly documented reading of emails functionality with python win32com outlook
我怎么只能得到 body
和 subject
任何其他东西要么返回 <COMObject <unknown>>
或以下错误.
How ever I can only get the body
and subject
anything else will either return <COMObject <unknown>>
or the following error.
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 对象,而不是一个字符串.它具有Name
和Address
等属性.请记住,并非收件箱中的所有项目都是 MailItem
对象 - 您也可以拥有 MeetingItem
和 ReportItem
对象.如果您只需要 MailItem
对象,请检查 Class
属性 = 43 (OlObjectClass.olMail)
Sender is indeed a COM object, not a string. It has properties like Name
and Address
. Keep in mind that not all items in your Inbox are MailItem
objects - you can also have MeetingItem
and ReportItem
objects. If you only want MailItem
objects, check that Class
property = 43 (OlObjectClass.olMail)
这篇关于python win32com Outlook,无法检索发件人信息.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!