我在这里做的是使用python从outlook获取电子邮件。问题是它在抓取时会收到随机的电子邮件。我想要的是以排序的方式从最旧的电子邮件到最新的电子邮件,这样我就可以以一种结构良好的形式将其存储到任何数据库中(如果有任何逻辑可以在一个循环中从最旧的到最新的按日期提取的话,这会更有用)。任何帮助都将不胜感激
def emailleri_al(folder):
messages = folder.Items ## want to add logic here
for message2 in messages:
Subject=message2.Subject
print(Subject)
for account in accounts:
if account.DisplayName=="mymail@gmail.com":
global inbox
inbox = outlook.Folders(account.DeliveryStore.DisplayName)
folders = inbox.Folders
for f in folders:
emailleri_al(f)
print("Finished Successfully")
调用
messages.Sort("[ReceivedTime]", false)
-参见https://docs.microsoft.com/en-us/office/vba/api/outlook.items.sort在Outlook中,迭代文件夹中的所有项目并不是一个好主意。相反,我建议使用
Items
类的Find
/FindNext
或Restrict
方法提取块中的项。您可以在以下系列文章中阅读有关这些方法的更多信息:Restrict
方法是使用Find
方法或FindNext
方法迭代集合中特定项的替代方法。如果有少量项,则Find
或FindNext
方法比过滤更快。如果集合中有大量项,则Restrict
方法的速度要快得多,尤其是如果在一个大集合中只希望找到少数项时例如,C#中列出的示例代码显示如何使用
Restrict
方法:因此,您可以在一个小的时间段内处理订购的项目
我也做过类似的工作:我需要按照收到的顺序列出所有带有特定主题的电子邮件,然后选择最新的一封。我使用了一个名为exchangelib的python库
下面是实现此功能的python代码段:
对于account.inbox.all().order_by('-datetime_received')[:100]:\35;对于您的要求,您只需删除[:100] 打印(item.subject、item.sender、item.datetime_received)
下面是完整的代码和逐步的解释
https://medium.com/@theamazingexposure/accessing-shared-mailbox-using-exchangelib-python-f020e71a96ab
相关问题 更多 >
编程相关推荐