使用Python/PowerShell读取Outlook邮件时无法访问正文与收件人问题
无法通过Python/PowerShell访问Outlook邮件正文与收件人属性问题
我尝试用Python和PowerShell读取Outlook邮件内容,能正常获取COM对象及邮件主题,但无法访问正文和收件人属性:PowerShell中对应属性返回$null,Python里则抛出com_error(-2147467260, 'Åtgärden avbröts', None, None)错误。
已尝试以下操作:
- 保持Outlook处于打开状态
- 在Outlook中手动选择邮件或不选择邮件
- 将单封邮件单独保存为文件后读取
已查阅多篇相关文章,但问题仍未解决,也找不到合理的原因解释。遗憾的是,我无法使用MS Graph或旧版Exchange PowerShell模块访问收件箱及邮件。
以下是我最新尝试的Python代码:
import win32com.client outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") for f in outlook.Folders('my@mail.se').Folders: if f.Name == "Inkorg": try: items = f.Items.Restrict("[MessageClass] = 'IPM.Note'") items.Sort("[ReceivedTime]", True) count = items.Count print(f"Found {count} items in 'Inkorg' folder.") for i in range(1, count + 1): try: item = items.Item(i) # Filter only mail items if item.Class == 43: # olMailItem = 43 print(f"Subject: {item.Subject}") sender = item.SenderName if item.SenderName else "Sender not found" html_body = item.HTMLBody if item.HTMLBody else "HTMLBody not found" print(f"Sender: {sender}") print(f"Body (HTML):\n{html_body[:200]}...") except Exception as e: print(f"Error accessing item {i}: {e}") except Exception as e: print(f"Error accessing folder '{f.Name}': {e}")
内容的提问来源于stack exchange,提问作者Smorkster
相关产品推荐
相关产品推荐

