簡體   English   中英

使用 Python 從 Outlook 獲取 Email 列表

[英]Get Email list from Outlook using Python

我正在嘗試獲取我在 outlook 中收到的最后 1000 封電子郵件。 但是代碼只從主文件夾而不是從子文件夾中獲取 Email。 請協助




import win32com.client
import pandas as pd
import dateutil.parser
from datetime import datetime

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
                                    # the inbox. You can change that number to reference
                                    # any other folder
messages = inbox.Items
messages.Sort("[ReceivedTime]", True)
i=1
df = pd.DataFrame(columns=['Sender','Subject','DateTime'])
Today = datetime.now().strftime("%m/%d/%Y") # current date and time

while i<1000:
    message=messages[i]
    DT1=message.ReceivedTime
    DT = DT1.strftime("%m/%d/%Y, %H:%M:%S")
    a=message.SenderEmailAddress
    if "-" in a:
        a=a.split("-",1)[1]
    b=message.subject

    df = df.append({'Sender':a,'Subject':b,'DateTime':DT}, ignore_index=True)
    i+=1
df.to_excel("C:/Users/abc/Downloads/Email.xlsx")

要對多個文件夾執行搜索,您需要使用Application class 的AdvancedSearch方法。 在 Outlook 中使用AdvancedSearch方法的主要好處是:

  • 搜索在另一個線程中執行。 您不需要手動運行另一個線程,因為AdvancedSearch方法會在后台自動運行它。
  • 可以在任何位置搜索任何項目類型:郵件、約會、日歷、便箋等,即超出某個文件夾的 scope。 RestrictFind / FindNext方法可以應用於特定的Items集合。
  • 完全支持 DASL 查詢(自定義屬性也可用於搜索)。 您可以在 MSDN 中的過濾文章中閱讀有關此內容的更多信息。 為了提高搜索性能,如果為商店啟用了即時搜索,則可以使用即時搜索關鍵字(請參閱Store類的IsInstantSearchEnabled屬性)。
  • 您可以隨時使用 Search class 的 Stop 方法停止搜索過程。

閱讀有關AdvancedSearch方法的更多信息並在 Outlook 中以編程方式查找示例:C#、VB.NET文章。

Items class 的RestrictFind / FindNext方法僅允許根據您的條件從單個文件夾中獲取項目。

暫無
暫無

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

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