使用Python从Outlook中获取邮件列表

问题描述 投票:0回答:1

我试图得到最后1000封邮件,我收到的Outlook。但该代码只从主文件夹获取电子邮件,而不是从子文件夹.请协助。




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")
python outlook pywin32
1个回答
0
投票

要在多个文件夹中进行搜索,你需要使用 "搜索 "功能。AdvancedSearch 的方法 Application 类。的主要好处。AdvancedSearch 方法在Outlook中是。

  • 搜索是在另一个线程中进行的。你不需要手动运行另一个线程,因为你的 AdvancedSearch 方法在后台自动运行。
  • 可以在任何位置搜索任何项目类型:邮件、约会、日历、备注等,即超出某个文件夹的范围。该 RestrictFindFindNext 方法可以应用到特定的 Items 集合。
  • 完全支持DASL查询(自定义属性也可用于搜索)。你可以在 筛选 MSDN中的文章。为了提高搜索性能,如果商店启用了即时搜索,可以使用即时搜索关键字(参见 IsInstantSearchEnabled 的财产 Store 类)。)
  • 你可以在任何时候使用Search类的Stop方法停止搜索过程。

阅读更多关于 AdvancedSearch 的方法,并找到样品在 在Outlook中以程序方式进行高级搜索。C#, VB.NET 文章:

该。RestrictFindFindNext 的方法 Items 类只允许根据你的条件从一个文件夹中获取物品。

© www.soinside.com 2019 - 2024. All rights reserved.