我正在尝试使用以下代码将一些电子邮件作为
.msg
文件从我的 Outlook 文件夹下载到我的本地计算机。
messages = source_folder.Items
for message in messages:
# Estimate the size of the email
size = message.Size
if size < 5 * 1024 * 1024: # 5MB
# Save the email as a .msg file
subject = message.Subject.replace(":", "").replace("\\", "").replace("/", "").replace("*", "").replace("?", "").replace("\"", "").replace("<", "").replace(">", "").replace("|", "")
received_time = message.ReceivedTime.strftime("%Y%m%d%H%M%S")
file_name = f"{subject}.msg"
file_path = os.path.join(email_dir, file_name)
time.sleep(10)
message.SaveAs(file_path) # 3 corresponds to the .msg format
print(f"Emails have been saved to {email_dir}")
我也得到了
com_error
。堆栈跟踪如下:
---------------------------------------------------------------------------
com_error Traceback (most recent call last)
Cell In[17], line 12
10 file_path = os.path.join(email_dir, file_name)
11 time.sleep(10)
---> 12 message.SaveAs(file_path) # 3 corresponds to the .msg format
13 if i == 0:
14 break
File <COMObject <unknown>>:2, in SaveAs(self, Path, Type)
com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The operation failed.', None, 0, -2147287037), None)
我已经研究了一些潜在的方法来做到这一点,但我无法解决它。任何解决方案将不胜感激。
-2147287037
是 STG_E_PATHNOTFOUND
,这很可能意味着文件夹 (email_dir
) 不存在。