Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python win32com outlook,can';无法检索发件人信息。_Python_Outlook_Pywin32 - Fatal编程技术网

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

python win32com outlook,can';无法检索发件人信息。,python,outlook,pywin32,Python,Outlook,Pywin32,我正在使用win32com.client与outlook交互。我已设法检索到邮件的正文和主题 我的代码基于以下帖子: 无论如何,我只能获取正文和主题任何其他内容都将返回或以下错误 Traceback (most recent call last): File "C:/Users/xx/PycharmProjects/email_crawler/email_crawler.py", line 62, in <module> main() File "C:/Users/x

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

我的代码基于以下帖子:

无论如何,我只能获取
正文
主题
任何其他内容都将返回
或以下错误

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

发送方实际上是一个COM对象,而不是字符串。它具有类似于
名称
地址
的属性。请记住,并非收件箱中的所有项目都是
MailItem
对象-您还可以拥有
MeetingItem
ReportItem
对象。如果您只想要
MailItem
对象,请检查
Class
property=43(OlObjectClass.olMail)

我在以前的项目中使用
message.sender
获取发件人信息时遇到问题。相反,我使用
message.SenderName
获取他们的姓名,并使用
message.SenderEmailAddress
获取他们的电子邮件地址。我还建议在发件人姓名上使用
unidecode
包,因为有些人的姓名中可能有特殊字符:
unidecode(message.SenderName)
。正在为MailItem属性添加一个。抱歉,这是一个非常新的东西。如何检查
类属性
是否为
43
?我尝试了
Sender.Name、.Sender.Address
如果message.Class==43,它们都返回相同的属性Error`来检查邮件项cals:

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()