6

我尝试使用exchangelib提取组邮箱进行分析,我想在一个日期范围内提取。
尝试使用过滤器功能,但似乎只适用于日历,请问您是否有任何电子邮件示例?谢谢大家。

4

2 回答 2

8

您需要过滤消息项上可用的日期时间字段。Message.FIELDS包含类上的所有可用字段Message。您可以列出所有日期时间字段,例如:

>>> [f.name for f in Message.FIELDS if f.value_cls == EWSDateTime]
['datetime_received', 'datetime_sent', 'datetime_created', 'reminder_due_by', 'last_modified_time']

自述文件显示了使用的示例.filter(start__range(x, y)),但该start字段仅适用于CalendarItem对象。相反,使用 egdatetime_received来过滤Message对象:

tz = EWSTimeZone.localzone()
emails_from_2017 = account.inbox.filter(datetime_received__range=(
    tz.localize(EWSDateTime(2017, 1, 1)),
    tz.localize(EWSDateTime(2018, 1, 1))
))
于 2018-02-12T08:59:02.090 回答
2
pytz_tz = pytz.timezone('Europe/Copenhagen') #setting the timezone

py_dt = pytz_tz.localize(datetime(year,month,day)) #building the custom date filter with a timezone object

ews_bfr = EWSDateTime.from_datetime(py_dt) #converting the custom date timezone object to a EWS (Exchange Web Service) date object


for item in account.inbox.all().order_by('-datetime_received')[:10000]: #look into the inbox the first 10K emails order desc by date received
    if item.datetime_received < ews_bfr: #if the mail if older than the custom date in the EWS format then apply rule
        item.delete() #delete all filtered emails
        print("Mail deleted Successfully")
于 2020-02-18T08:15:16.567 回答