gpt4 book ai didi

python - 使用 python 提取 outlook 电子邮件正文和收件人电子邮件地址

转载 作者:太空宇宙 更新时间:2023-11-04 04:36:17 24 4
gpt4 key购买 nike

我正在尝试提取 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)

我正在尝试的 body :

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)

最佳答案

我刚刚在我的计算机上运行了类似的代码,在一个包含 3,000 多个混合类型项目(Skype 消息通知、日历邀请/通知、电子邮件等)的收件箱中,我无法重现此错误,即使对于包含以下内容的项目也是如此not m.Body——我认为这可能是罪魁祸首,也许某种类型的元素没有主体会引发错误——但事实似乎并非如此:

>>> for m in messages:
... if not m.body:
... print(m.Subject)
... print(m.Body)
...
Accepted: Tables discussion

Message Recall Failure: tables/ new data status

Message Recall Failure: A few issues with the data

您可能应该添加一个 print(m.Class),因为我仍然认为某些类型的项目可能没有 Body 属性。

This thread建议可能存在阻止以编程方式访问 Outlook 的用户/安全设置,因此您可能需要仔细检查(尽管我认为如果不允许,您的代码都不会起作用——仍然值得研究! ).

I have figured out the source of this error. We are running into issues with the comObjectModelGaurd. Our group policy recently changed to disallow programatic access to protected mailItem objects.

Modifying Outlook Users Trust settings or the registry will fix the problem.

既然我无法复现错误,也许我还能帮你调试并找出问题的根源,从中我们可能会想出一个好的解决方案。

使用一个函数来获取项目的主体,并使用 try/except 来识别哪些项目导致了错误。

def getBody(m):
s = ""
try:
s = m.Body
except COMError:
s = '\t'.join(('Error!', m.Class, m.senton.date(), m.Subject))
return s


for m in messages:
print(getBody(m))

关于python - 使用 python 提取 outlook 电子邮件正文和收件人电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51634576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com