如果使用python在电子邮件正文中存在关键字,如何检索outlook中的第一封电子邮件?

发布于 2025-01-12 23:00:38 字数 682 浏览 0 评论 0原文

如果使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港巷口少年归 2025-01-19 23:00:38

Items.Sort 需要至少一个参数(字符串) - 要排序的属性名称(请参阅 https://learn.microsoft.com/en-us/office/vba/api/outlook.items.sort)。然后,您可以使用 Items.Find() 搜索 PR_BODY MAPI 属性:

messages=messages.sort('ReceivedTime', true)
message=messages.Find('@SQL="http://schemas.microsoft.com/mapi/proptag/0x1000001F" LIKE \'%Test%\' ')

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 use Items.Find() to search on the PR_BODY MAPI property:

messages=messages.sort('ReceivedTime', true)
message=messages.Find('@SQL="http://schemas.microsoft.com/mapi/proptag/0x1000001F" LIKE \'%Test%\' ')
蓦然回首 2025-01-19 23:00:38

无需使用 Restrict 方法,然后调用 GetLast 方法。因此,可以使用 Find 方法重写以下代码:

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 + "'")

首先,您需要向搜索字符串(VBA)添加附加条件:

messages=inbox.Items
messages.Sort "[ReceivedTime]", False
'Create DASL query for Body using content indexing phrase match for 'office' 
 filter = "@SQL=" + "\""
            + "urn:schemas:httpmail:textdescription" + "\""
            + " like '%office%'";
firstMessage = messages.Find(filter)

您可能会找到 搜索对于文件夹中的项目正文中的短语文章很有帮助。

There is no need to use the Restrict method and then call the GetLast one. So, the following code could re-written using the Find method:

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 + "'")

First, you need to add additional condition to the search string (VBA):

messages=inbox.Items
messages.Sort "[ReceivedTime]", False
'Create DASL query for Body using content indexing phrase match for 'office' 
 filter = "@SQL=" + "\""
            + "urn:schemas:httpmail:textdescription" + "\""
            + " like '%office%'";
firstMessage = messages.Find(filter)

You may find the Search for a phrase in the body of items in a folder article helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文