使用win32com.client列出可用的消息属性

3
我有一个文件夹,里面存储了数百封Outlook电子邮件,格式为.msg。我有一个脚本可以访问特定属性,例如SenderName、Subject等,但是在某些文件上脚本会失败。经过一些调查,发现失败的文件是对日历邀请的响应,并且失败的属性是“To”属性,因此这必须在电子邮件和日历邀请之间进行不同处理。所有文件都以.msg格式存储,因此没有明显的方法可以区分每个文件而不打开每个单独的文件。

我发现一些其他帖子重点关注直接访问Outlook,而不是使用保存在目录中的.msg文件。我目前已经硬编码了一些内容(见下面的代码),但是这个练习已经证明属性可以根据文件类型表现出不同的行为。

我拥有的部分代码如下所示:

for eachFile in msgList:
    filePath = outDir + "\\" + eachFile
    msg = outlook.OpenSharedItem(filePath)
    print msg.ReceivedTime
    print msg.Subject
    print msg.Body
    print msg.To
    print msg.Size
    print msg.Attachments

有没有一种方法列出每个文件的所有可用属性?或者有没有一种方法可以区分消息的类型,例如电子邮件、日历?
3个回答

1

在访问任何邮件属性之前,请检查 Class 属性(由所有 OOM 对象公开)- 对于 MailItem 对象,它将是 43 (olMailItem)。


有没有理由更喜欢Class而不是MessageClass?我注意到所有Class == 43的项目都以IPM.Note开头,但其中一些Class == 43的项目包括语音邮件之类的东西。 - undefined
类(43)是Outlook对象(MailItem)的类型。MessageClass仅适用于项目(即适用于文件夹、附件等),并且更加细致。例如,“IPM.Note”消息和“IPM.Note.VoiceMail”可能由Outlook对象模型中的同一对象(MailItem)表示,但Outlook可以显示显示这两个对象的不同形式-您可以使用自己的消息类并动态地将其与自己的表单关联,而两者都将由MailItem对象表示,因为您无法动态修改Outlook对象模型库,但可以自由创建自己的表单。 - undefined

0
print(message.__dir__())

输出:


['TaskStartDate', '_username_', '_print_details_', '__lt__', '__doc__', 'ReceivedByEntryID', '_FlagAsMethod', 'MessageClass', '_get_good_single_object_', 'BCC', 'UserProperties', '_lazydata_', '__weakref__', 'Display', 'ReminderTime', 'ReminderSoundFile', 'RetentionPolicyName', 'InternetCodepage', '__ne__', '_mapCachedItems_', 'Forward', 'Sent', 'SaveSentMessageFolder', 'ConversationTopic', '__reduce_ex__', '__call__', 'MarkForDownload', 'IsMarkedAsTask', 'ReplyAll', '__LazyMap__', 'Invoke', 'ToDoTaskOrdinal', 'ClearTaskFlag', '_enum_', 'PermissionService', '__hash__', '__eq__', 'RemoteStatus', 'DeleteAfterSubmit', '__repr__', 'Session', 'AddRef', 'Actions', 'Subject', '__getattribute__', 'SenderName', '__subclasshook__', '__setattr__', 'Copy', 'HTMLBody', 'ReplyRecipientNames', 'VotingResponse', 'Reply', 'Links', 'Companies', '_find_dispatch_type_', '__gt__', 'OutlookVersion', '_UpdateWithITypeInfo_', 'Saved', 'ReminderOverrideDefault', 'VotingOptions', 'Conflicts', '_make_method_', 'SendUsingAccount', '_oleobj_', '__len__', 'Close', 'Move', '_proc_', 'Mileage', 'Permission', '_dir_ole_', 'LastModificationTime', 'ReceivedByName', 'ItemProperties', 'IsIPFax', 'FlagStatus', '__reduce__', 'GetTypeInfo', 'ConversationID', 'Sender', '__init__', '__ge__', 'DownloadState', 'PermissionTemplateGuid', 'Application', 'SaveAs', 'TaskCompletedDate', 'AutoResolvedWinner', 'SentOnBehalfOfName', 'GetIDsOfNames', 'NoAging', '_LazyAddAttr_', 'TaskDueDate', '_get_good_object_', 'RTFBody', 'Size', 'CreationTime', 'AutoForwarded', 'Class', 'Sensitivity', 'Importance', '_ApplyTypes_', '__new__', '__int__', 'CC', 'ExpiryTime', 'To', 'ConversationIndex', '__AttrToID__', 'BodyFormat', 'OutlookInternalVersion', 'AlternateRecipientAllowed', 'ReadReceiptRequested', 'PropertyAccessor', 'RetentionExpirationDate', 'EntryID', 'Parent', '_wrap_dispatch_', 'Body', '__str__', '_NewEnum', '__bool__', '__dict__', '__format__', 'EnableSharedAttachments', 'FlagRequest', 'ShowCategoriesDialog', '__getitem__', 'HasCoverSheet', 'Attachments', '_unicode_to_string_', '_Release_', '__class__', 'GetConversation', 'FlagDueBy', '__setitem__', '__delattr__', '__module__', '__dir__', 'Release', 'UnRead', 'ReceivedOnBehalfOfEntryID', '_builtMethods_', '__le__', 'DeferredDeliveryTime', 'BillingInformation', 'ReminderPlaySound', 'SenderEmailAddress', 'QueryInterface', 'Categories', 'Save', 'SenderEmailType', 'Recipients', 'Delete', 'MAPIOBJECT', 'Send', 'FlagIcon', '__getattr__', 'SentOn', 'AddBusinessCard', 'ClearConversationIndex', 'GetTypeInfoCount', 'GetInspector', 'IsConflict', 'ReplyRecipients', 'PrintOut', 'ReminderSet', 'ReceivedTime', '__init_subclass__', '__sizeof__', 'FormDescription', '_olerepr_', 'OriginatorDeliveryReportRequested', 'MarkAsTask', 'ReceivedOnBehalfOfName', 'RecipientReassignmentProhibited', 'Submitted', 'TaskSubject'] 

0

嗨。尽管链接可能很有用,但在StackOverflow上的答案也需要能够独立存在。请添加解释和您推荐的输出结果。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接