2014-10-12 207 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)