要使用python将outlook邮件从最旧的邮件提取到最新的邮件吗

2024-10-04 07:28:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在这里做的是使用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")

Tags: infor电子邮件accountfolderfolderssubjectmessages
3条回答

调用messages.Sort("[ReceivedTime]", false)-参见https://docs.microsoft.com/en-us/office/vba/api/outlook.items.sort

在Outlook中,迭代文件夹中的所有项目并不是一个好主意。相反,我建议使用Items类的Find/FindNextRestrict方法提取块中的项。您可以在以下系列文章中阅读有关这些方法的更多信息:

Restrict方法是使用Find方法或FindNext方法迭代集合中特定项的替代方法。如果有少量项,则FindFindNext方法比过滤更快。如果集合中有大量项,则Restrict方法的速度要快得多,尤其是如果在一个大集合中只希望找到少数项时

例如,C#中列出的示例代码显示如何使用Restrict方法:

private void RestrictCalendarItems(Outlook.MAPIFolder folder)
{
    DateTime dtEnd = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
                                  DateTime.Now.Day, 23, 59, 00, 00);
    string restrictCriteria = "[Start]<=\"" + dtEnd.ToString("g") + "\"" +
                              " AND [End]>=\"" + DateTime.Now.ToString("g") + "\"";
    StringBuilder strBuilder = null;
    Outlook.Items folderItems = null;
    Outlook.Items resultItems = null;
    Outlook._AppointmentItem appItem = null;
    int counter = default(int);
    object item = null;
    try
    {
        strBuilder = new StringBuilder();
        folderItems = folder.Items;
        folderItems.IncludeRecurrences = true;
        folderItems.Sort("[Start]");
        resultItems = folderItems.Restrict(restrictCriteria);
        item = resultItems.GetFirst();
        do
        {
           if (item != null)
           {
               if (item is Outlook._AppointmentItem)
               {
                   counter++;
                   appItem = item as Outlook._AppointmentItem;
                   strBuilder.AppendLine("#" + counter.ToString() +
                                         "\tStart: " + appItem.Start.ToString() +
                                         "\tSubject: " + appItem.Subject +
                                         "\tLocation: " + appItem.Location);
               }
               Marshal.ReleaseComObject(item);
               item = resultItems.GetNext();
           }
       }
       while (item != null);
       if (strBuilder.Length > 0)
           Debug.WriteLine(strBuilder.ToString());
       else
           Debug.WriteLine("There is no match in the "
                            + folder.Name + " folder.");
   }
   catch (Exception ex)
   {
       System.Windows.Forms.MessageBox.Show(ex.Message);
   }
   finally
   {
       if (folderItems != null) Marshal.ReleaseComObject(folderItems);
       if (resultItems != null) Marshal.ReleaseComObject(resultItems);
   }
}

因此,您可以在一个小的时间段内处理订购的项目

我也做过类似的工作:我需要按照收到的顺序列出所有带有特定主题的电子邮件,然后选择最新的一封。我使用了一个名为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

相关问题 更多 >