【发布时间】:2019-01-09 02:25:45
【问题描述】:
我正在尝试提取 Outlook 电子邮件正文、收件人地址、主题和接收日期。
我能够提取主题和接收日期,但无法提取正文和收件人地址:
以下是我的主题代码和接收日期:
outlook = win32com.client.Dispatch('Outlook.Application').GetNamespace('MAPI')
namespace = outlook.Session
recipient = namespace.CreateRecipient("abc@xyz.com")
inbox = outlook.GetSharedDefaultFolder(recipient, 6)
messages = inbox.Items
email_subject = []
email_date = []
email_date_time = []
for x in messages:
sub = x.Subject
received_date = x.senton.date()
received_date_time = str(x.ReceivedTime)
email_subject.append(sub)
email_date.append(received_date)
email_date_time.append(received_date_time)
对于我正在尝试的身体:
for x in messages:
body = x.Body
print(body)
但这不起作用,我收到以下错误:
Traceback (most recent call last):
File "<ipython-input-85-d79967933b99>", line 2, in <module>
sub = x.Body
File "C:\ProgramData\Anaconda3\lib\site-packages\win32com\client\dynamic.py", line 516, in __getattr__
ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
com_error: (-2147467259, 'Unspecified error', None, None)
【问题讨论】:
-
“这不起作用”是什么意思?请包含错误的完整堆栈跟踪,或者如果没有错误,您需要描述实际输出可能与您期望的不同。
-
我收到此错误:回溯(最近一次调用最后一次):文件“
”,第 2 行,在 sub = x.Body 文件“C :\ProgramData\Anaconda3\lib\site-packages\win32com\client\dynamic.py",第 516 行,在 getattr 中 ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1 ) com_error: (-2147467259, '未指定的错误', 无, 无) -
另外,不应该是
received_date = x.SentOn吗?我很确定senton.date()在 Outlook 对象模型中无效。 Python 对于初学者来说是区分大小写的,并且没有date属性或SentOn属性的方法。 -
此外,您在评论中提供的回溯与您提供的代码的任何部分都不一致。请修改您的问题,使其连贯。
-
received_date = x.senton.date()....这对我来说非常好用。我也更新了问题。
标签: python email outlook win32com