【问题标题】:Reading saved email file with “.msg” extension, in local disk在本地磁盘中读取已保存的带有“.msg”扩展名的电子邮件文件
【发布时间】:2018-07-03 04:33:36
【问题描述】:

如何阅读电子邮件文件(保存到本地驱动器的电子邮件,扩展名为“.msg”)?

我尝试了这 2 行,但没有成功。

msg = open('Departure  HOUSTON EXPRESS  Port  NORFOLK.msg', 'r')
print msg.read()

我在网上搜索了一个答案,它给出了以下代码:

import email
def read_MSG(file):
    email_File = open(file)
    messagedic = email.Message(email_File)
    content_type = messagedic["plain/text"]
    FROM = messagedic["From"]
    TO = messagedic.getaddr("To")
    sujet = messagedic["Subject"]
    email_File.close()
    return content_type, FROM, TO, sujet


myMSG= read_MSG(r"c:\\myemail.msg")
print myMSG

但是它给出了一个错误:

Traceback (most recent call last):
File "C:\Python27\G.py", line 19, in <module>
myMSG= read_MSG(r"c:\\myemail.msg")
File "C:\Python27\G.py", line 10, in read_MSG
messagedic = email.Message(email_File)
TypeError: 'LazyImporter' object is not callable

互联网上的一些回复告诉我们最好在解析之前将 .msg 转换为 .eml,但我不太确定如何。

读取 .msg 文件的最佳方式是什么?

【问题讨论】:

  • 错误信息中的完整回溯是什么?
  • 谢谢,约翰·兹温克。请查看更新。

标签: python email parsing


【解决方案1】:

您现在拥有的代码看起来对于您要完成的工作完全不可行。您需要解析 Outlook ".msg" 文件,在 Python 中 can be done 但不使用 email 模块。但是,如果您可以使用您提到的“.eml”文件,那会更容易,因为email 模块可以读取这些文件。

要读取 .eml 文件,请参阅 email.message_from_file()

【讨论】:

  • 谢谢,茨温克先生。顺便问一下,如何将“.msg”转换为“.eml”?
  • 我不知道,你已经说过你在网上找到了解释如何做到这一点的资源。或者也许 Outlook 可以做到。不确定。
猜你喜欢
  • 2011-09-07
  • 2014-11-26
  • 2011-09-22
  • 2014-06-30
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 2014-10-30
相关资源
最近更新 更多