简体   繁体   中英

Access email from a particular folder in Outlook, within a specific date range (user Input) using "exchangelib" in Python

Summary of the requirement: To access emails from a specific folder in Outlook within a user given date range,

ex: all mails from June or all mails from 23-June-2020 to 15-July-2020

So far we have tried the following but the issues are:

  1. Date range is not giving correct output
  2. It is taking too long to give output, also sometimes returning with a timeout error.

The Code:

from exchangelib import Credentials, Account, DELEGATE, Configuration, IMPERSONATION, FaultTolerance,EWSDateTime,EWSTimeZone,Message
import datetime 
import pandas as pd


new_password = "your password"

credentials = Credentials(username = 'username', password = new_password)
config = Configuration(server ='outlook.office365.com', credentials = credentials)
account = Account(primary_smtp_address ='username', credentials = credentials, autodiscover = False, config = config, access_type = DELEGATE)

#first approach.....................................

conversation_id = []
datetime_received = []
has_attachment = []
Senders = []

for i in account.inbox.all().order_by('-datetime_received')[:40]:
    if isinstance(i, Message):
        if i.datetime_received:
            if ((i.datetime_received).year == 2020):
                if ((i.datetime_received).month == 7):
                        if i.conversation_id:
                            print("conversation id: ", i.conversation_id.id)
                            conversation_id.append(i.conversation_id.id)
                        if not i.conversation_id:
                            conversation_id.append("Not available")

                        if i.sender:
                            print("Sender name : ", i.sender.name)
                            Senders.append(i.sender.name)
                        if not i.sender:
                            Senders.append("not available")

                        if i.datetime_received:
                            print("Time : ", i.datetime_received.date)
                            datetime_received.append(i.datetime_received.date)
                        if not i.datetime_received:
                            datetime_received.append("not available")

                        if i.has_attachments:
                            print("Has attachment: ", i.has_attachments)
                            has_attachment.append(i.has_attachments)
                        if not i.has_attachments:
                            has_attachment.append("not available")
                    
                    

# second approach.....................................................................
items_for_2019 = account.inbox.filter(start__range=(
    tz.localize(EWSDateTime(2019, 8, 1)),
    tz.localize(EWSDateTime(2020, 1, 1))
)) 

for i in items_for_2019:
    print("")



#third approach.........................................................................

for item in account.inbox.filter(datetime_received__range=(tz.localize(EWSDateTime(2019, 8, 1)),tz.localize(EWSDateTime(2020, 1, 1)))):
    print(item.sender.name)

first approach is working for specific month but extremely slow

second and third approach giving wrong output

A little guidance would be highly appreciated.

The start field belongs to CalendarItem . It is not a valid field for Message objects. That's why your 2nd approach does not work.

Your 3rd approach should work. Which output do you see, and what did you expect?

If your queries are taking a long time, try to limit the fields you are fetching for each item, by using the .only() QuerySet method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM