【问题标题】:Sudden error when retrieving emails from Outlook using Python and win32com使用 Python 和 win32com 从 Outlook 检索电子邮件时出现突发错误
【发布时间】:2019-06-01 15:53:20
【问题描述】:

编辑:解决了!

我在下面添加了这段代码的 sn-p,以便跟踪主收件箱文件夹的位置

outlook.Folders 中的文件夹: 打印(文件夹)

这突出表明基础 Outlook 结构中发生了一些变化,文件夹 [0] 不再有效。我现在将调整代码以使其更健壮并动态选择文件夹

结束编辑

我编写了一些代码来从 Outlook 中提取电子邮件并保存附件。直到几天前它都运行良好。

我没有接触过代码,所以我只能假设 Outlook 中的某些内容发生了变化。我在公司环境中工作,所以需要远程更新软件。

有谁知道这个错误是什么意思,为什么突然出现?我很沮丧,因为代码在这个打嗝之前运行得很好。或者,有什么更好的方法可以使用 Python 从 Outlook 中检索电子邮件和附件?

import win32com.client


def main():
    pass


def saveAttachments():
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")  # Opens Microsoft Outlook
    mailbox = outlook.Folders[0]  # Based off email address
    inbox = mailbox.Folders["Inbox"]
    emails = inbox.Items
    emails.Sort("[ReceivedTime]", True)

    destPath = "\\\\servername\\path\\"

    try:
        for mail in emails:
            if ("Detailed MTM," in mail.subject) and (mail.Attachments.Count > 0):
                print(mail.Sender)
                print(mail.Subject)
                print(mail.Receivedtime)
                attachments = mail.Attachments
                for file in attachments:
                    if "MTMDetailed" in str(file):
                        file.SaveAsFile(destPath + str("MTMDetailed.xls"))
                break
    except:
        file = open(destPath + "error.log", "w")
        file.write("Problem")
        file.close()

if __name__ == '__main__':
    main()
    saveAttachments()

  File "C:\Tools\Python\lib\site-packages\win32com\client\dynamic.py", line 256, in __getitem__
    return self._get_good_object_(self._oleobj_.Invoke(dispid, LCID, invkind, 1, index))
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The attempted operation failed.  An object could not be found.', None, 0, -2147221233), None)

【问题讨论】:

  • 尝试将.Folders[0]更改为.Folders[1]
  • 怕运气不好,还是会崩溃。
  • 一个明显的问题 - \\servername\path\ 是否可访问?

标签: python python-3.x outlook


【解决方案1】:

我添加了代码来遍历 Outlook.Folders 以找到我需要的文件夹,而不依赖于特定的硬编码位置

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-20
    • 2021-12-01
    • 2018-05-30
    • 2017-05-25
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多