如何使用 python 保存 Outlook 中的电子邮件
我正在尝试使用下面的 python 脚本保存子文件夹中的电子邮件,我正在尝试使用 days=1
进行限制,这意味着我只需要保存 1 天前的电子邮件。
from win32com.client import Dispatch
from datetime import date, timedelta
import datetime as dt
msg_location = r'C:\Users\rahul\Desktop\msg_files'
outlook = Dispatch("outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6).Folders['Email_snapper']
messages = inbox.Items
message = messages.GetFirst()
Body_content = message.Body
print(Body_content)
for msg in messages:
lastWeekDateTime = dt.datetime.now() - dt.timedelta(days=1)
lastWeekDateTime = lastWeekDateTime.strftime('%m/%d/%Y %H:%M %p')
message = messages.Ryestrict("[ReceivedTime] >= '" + lastWeekDateTime + "'")
#name = str(message)
message.SaveAs(msg+".msg")
I am trying to save email from sub-folder using the below python script, I am trying to restrict with days=1
means I only need to save emails which are 1 day old.
from win32com.client import Dispatch
from datetime import date, timedelta
import datetime as dt
msg_location = r'C:\Users\rahul\Desktop\msg_files'
outlook = Dispatch("outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6).Folders['Email_snapper']
messages = inbox.Items
message = messages.GetFirst()
Body_content = message.Body
print(Body_content)
for msg in messages:
lastWeekDateTime = dt.datetime.now() - dt.timedelta(days=1)
lastWeekDateTime = lastWeekDateTime.strftime('%m/%d/%Y %H:%M %p')
message = messages.Ryestrict("[ReceivedTime] >= '" + lastWeekDateTime + "'")
#name = str(message)
message.SaveAs(msg+".msg")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试像下面这样设置您的过滤器
示例
Try setting your filter like the following
Example
首先,它是
Restrict
,而不是Ryestrict
。其次,
Restrict
返回Items
集合,而不是单个项目。您需要迭代该集合中的项目。如果您只需要单个项目,请使用Find
而不是Restrict
。Firstly, it is
Restrict
, notRyestrict
.Secondly,
Restrict
returnsItems
collection, not a single item. You need to iterate over the items in that collection. If you only expect a single item, useFind
instead ofRestrict
.