簡體   English   中英

想要使用 python 從最舊的郵件中獲取 outlook 郵件到最新的郵件

[英]Want to fetch outlook mails from the oldest mail to the newest mail using 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")

調用messages.Sort("[ReceivedTime]", false) - 請參閱https://docs.microsoft.com/en-us/office/vba/api/outlook.items.sort

在 Outlook 中迭代文件夾中的所有項目並不是一個好主意。 相反,我建議使用Items class 的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);
   }
}

因此,您可以在很小的時間段內處理訂購的物品。

我處理了類似的要求:我需要按照收到的順序列出所有具有特定主題的 email - 然后選擇最新的。 我使用了一個名為 exchangelib 的 python 庫。

這是執行此操作的 python 片段:

for item in account.inbox.all().order_by('-datetime_received') [:100] : #for your requirements you just need to remove [:100] print(item.subject, item.sender, item.datetime_received)

這是完整的代碼,一步一步的解釋。

https://medium.com/@theamazingexposure/accessing-shared-mailbox-using-exchangelib-python-f020e71a96ab

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM