2014-10-12 249 views

回答

12

這個工作對我來說:

import win32com.client 
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") 
msg = outlook.OpenSharedItem(r"C:\test_msg.msg") 

print msg.SenderName 
print msg.SenderEmailAddress 
print msg.SentOn 
print msg.To 
print msg.CC 
print msg.BCC 
print msg.Subject 
print msg.Body 

count_attachments = msg.Attachments.Count 
if count_attachments > 0: 
    for item in range(count_attachments): 
     print msg.Attachments.Item(item + 1).Filename 

del outlook, msg 
+4

重要的是要注意OpenSharedItem方法需要一個絕對路徑,否則你會得到一個錯誤。 – smartexpert 2016-06-17 08:58:01

+1

我似乎有編碼問題。你怎麼解決這個問題? – firko 2017-03-01 20:39:56

3

即使這是一個古老的線程,我希望這個信息可以幫助的人誰是尋找一個解決方案,以什麼的線程主題正是說。我強烈建議使用mattgwwalker in github的解決方案,這需要在外部安裝OleFileIO_PL module

0

我已經試過了蟒蛇電子郵件模塊,有時並不成功解析味精文件。

所以,在這種情況下,如果你只是在文本或HTML之後,下面的代碼爲我工作。

start_text = "<html>" 
end_text = "</html>" 
def parse_msg(msg_file,start_text,end_text): 
    with open(msg_file) as f: 
    b=f.read() 
    return b[b.find(start_text):b.find(end_text)+len(end_text)] 

print parse_msg(path_to_msg_file,start_text,end_text)