2015-06-22 110 views
0

現在,我有一些代碼執行以下操作獲取附件的哈希.msg文件

  1. 獲取從收件箱中的郵件
  2. 下載電子郵件
  3. 經過每一個被下載的電子郵件,並提取附件
  4. 保存附件
  5. 獲取所保存的文件的哈希

但不是保存附件,是否有可能只獲取附件的散列並跳過保存?

attachments = message.Attachments 
for attachment in attachments: 
    if attachment.FileName.endswith(".msg"): 
     attachment.SaveASFile(
      os.path.dirname(os.path.abspath(__file__)) + '/attachments/' + str(
       time.time()) + "-" + message.subject + "-" + attachment.FileName) 

編輯:

這是我已經試過:

def get_attachments(message): 
    attachments = message.Attachments 
    for attachment in attachments: 
     data_array = message.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37010102") 
     print len(data_array) 

這是我得到的錯誤:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, u'Microsoft Outlook', u'The property "http://schemas.microsoft.com/mapi/proptag/0x37010102" is unknown or cannot be found.', None, 0, -2147221233), None) 

Process finished with exit code 1 

回答

0

是的,這是可能的。您需要使用Attachment類的PropertyAccessor屬性來獲取相應類的實例。它返回一個支持創建,獲取,設置和刪除父Attachment對象屬性的PropertyAccessor對象。因此,您可以使用PropertyAccessor類的GetProperty方法來獲取PR_ATTACH_DATA_BIN屬性的值(DASL名稱爲http://schemas.microsoft.com/mapi/proptag/0x37010102),該值返回表示附加文件的字節數組。

+0

嗯,我得到一個奇怪的錯誤,這將編輯我的問題,以反映我的反饋。 –

+0

你在PC上安裝了哪些Outlook版本?事實是,PropertyAccessor不允許在舊的Outlook版本中獲得巨大的值。您需要使用低級別的API - 擴展MAPI或圍繞該API的任何其他包裝器(例如Redemption)。 –

+0

您是否在獲取PR_ATTACH_DATA_BIN屬性值之前簽出Type屬性? –

0

PR_ATTACH_DATA_BIN只能用於常規按值附件。嵌入的消息或OLE附件沒有該屬性。確保Attachment.Type屬性== 1(OlAttachmentType.olByValue)。

如果是嵌入式消息附件,可以將其保存爲臨時MSG文件並計算其散列值,但請注意,MSG文件將包含日期/時間戳,並且每個散列值都會產生一個新的散列值時間MSG文件被保存。