如果使用python在电子邮件正文中存在关键字,如何检索outlook中的第一封电子邮件?
如果使用 python 的电子邮件正文中存在某个关键字,我将不胜感激,了解如何将弹出的第一封电子邮件的电子邮件正文导出到 Excel 文件中。
到目前为止我已经成功访问了子文件夹,下面是我的代码。
import win32com.client
outlook=win32com.client.Dispatch('outlook.application')
mapi=outlook.GetNamespace("MAPI")
for account in mapi.Accounts:
#Passing in the account name
print(account.DeliveryStore.DisplayName)
inbox=mapi.GetDefaultFolder(6).Folders['Email']
messages=inbox.Items
received_dt=datetime.now()-timedelta(days=10)
received_dt=received_dt.strftime('%m/%d/%Y %H:%M %p')
messages = messages.Restrict("[ReceivedTime] >= '" + received_dt + "'")
message=messages.GetLast()
body_content=message.body
print(body_content)
Would appreciate any help on how I can export the email body of the first email that pops out into a excel file if a certain keyword exists in the email body using python.
I have managed to access the sub folder so far, below is my code.
import win32com.client
outlook=win32com.client.Dispatch('outlook.application')
mapi=outlook.GetNamespace("MAPI")
for account in mapi.Accounts:
#Passing in the account name
print(account.DeliveryStore.DisplayName)
inbox=mapi.GetDefaultFolder(6).Folders['Email']
messages=inbox.Items
received_dt=datetime.now()-timedelta(days=10)
received_dt=received_dt.strftime('%m/%d/%Y %H:%M %p')
messages = messages.Restrict("[ReceivedTime] >= '" + received_dt + "'")
message=messages.GetLast()
body_content=message.body
print(body_content)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Items.Sort
需要至少一个参数(字符串) - 要排序的属性名称(请参阅 https://learn.microsoft.com/en-us/office/vba/api/outlook.items.sort)。然后,您可以使用Items.Find()
搜索PR_BODY
MAPI 属性:Items.Sort
requires at least one argument (string) - the name of the property to sort on (see https://learn.microsoft.com/en-us/office/vba/api/outlook.items.sort). You can then useItems.Find()
to search on thePR_BODY
MAPI property:无需使用
Restrict
方法,然后调用GetLast
方法。因此,可以使用Find
方法重写以下代码:首先,您需要向搜索字符串(VBA)添加附加条件:
您可能会找到 搜索对于文件夹中的项目正文中的短语文章很有帮助。
There is no need to use the
Restrict
method and then call theGetLast
one. So, the following code could re-written using theFind
method:First, you need to add additional condition to the search string (VBA):
You may find the Search for a phrase in the body of items in a folder article helpful.