2

我正在尝试使用 win32com 从 Outlook 打印电子邮件。唯一的问题是当我尝试访问MailItem.SentOnMailItem.ReceivedTime时,Python 崩溃,窗口显示“Python 已停止工作”对话框窗口。这个问题的原因可能是什么?

这是我的代码:

import win32com
import win32com.client
import os
import sys
import re
from datetime import datetime

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

inbox = outlook.Folders(accounts[0].DeliveryStore.DisplayName)
folders = inbox.Folders

inbox_messages = folders("Inbox").Items
msg = inbox_messages.GetFirst()

while msg:
    print(msg.SenderEmailAddress)
    print(msg.Subject)
    print (msg.SentOn)
    msg = inbox_messages.GetNext()

我在 Windows 10 上使用 Office 365 和 Python 3.7.0。

错误窗口

4

2 回答 2

1

我有同样的问题。我的脚本在 Python 3.6.5 中运行良好,但在 3.7.0 中失败

于 2018-09-04T19:58:39.553 回答
0

所以不是所有的邮件都会有 SenderEmailAddress 和 SentOn。我在我的收件箱上尝试了你的程序,它似乎在 Outlook 生成的邮件上崩溃了 - Outlook 生成的无法投递的邮件:

Your message to xxxx@xxxx.com couldn't be delivered.
A custom mail flow rule created by an admin at xxx.onmicrosoft.com has blocked your message.

.

Traceback (most recent call last):
  File "xxxx\outl.py", line 24, in <module>
    print (msg.SentOn)
  File "xxx\lib\site-packages\win32com\client\dynamic.py", line 527, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: GetNext.SentOn

不知道为什么它会为您崩溃 python,但对我来说会导致堆栈崩溃,但我认为您应该处理可能缺少属性的情况。

于 2018-08-17T20:12:13.500 回答