0

I'm trying to get the last email receirved using exchangelib listener;

the probleme here is that the code not printing the seconde print(account.inbox.all().count()) ,

and the first print(account.inbox.all().count()) printinig it fine,

see the result below the code

creds = Credentials(
    username="domaine\\user", 
    password="password"
)
def main():
    print("started !")
    config = Configuration(server='server', credentials=creds)

    account = Account(
        primary_smtp_address="mail",
        autodiscover=False, 
        config=config,
        access_type=DELEGATE,
        default_timezone=UTC
    )

    listener = Listener(account)

    print(account.inbox.all().count())

    def new_messaged_received():
        print("---------------------------------new mail arrived----------------------------------------------");
        for item in account.inbox.all().only('subject').order_by('-datetime_received')[:1]:
            print(item.subject) 

    listener.streaming_event_received += new_messaged_received
    listener.listen(NewMailEvent)

the result after receirving a new email :

7503
---------------------------------new mail arrived----------------------------------------------
bfs
  • 1
  • 1
  • if Listener executes `new_messaged_received` when you run `account.inbox.all().count()` then another `account.inbox.all().count()` inside `new_messaged_received` may execute another `new_messaged_received`, which may again run another `new_messaged_received`, etc. and this can make recursion which may block all code. It may need to remove `account.inbox.all().count()` from `new_messaged_received`, or it may need some method to disable `Listener` for time when you run `account.inbox.all().count()` inside `new_messaged_received` – furas Sep 27 '22 at 15:03
  • thank you for replying. the exact code that i want to run inside the listener is `for item in account.inbox.all().only('subject').order_by('-datetime_received')[:1]: print(item.subject)` i was just testing using the count, but they both blocked inside the listener, and working fine outside the listener – bfs Sep 27 '22 at 15:28
  • do you have this problem when you remove this line? How do you add `new_messaged_received` to `listener`? I don't have access to `exchange` but maybe you should create minimal working code so someone else could test it. – furas Sep 27 '22 at 15:43
  • i just edited my code so you can see.. now when in get a new email, it prints "new message arrived" but the line `for item in account.inbox.all().only('subject').order_by('-datetime_received')[:1]: print(item.subject)` its not working, or rather it distrupts the script, that is when a new email comes in , nothing is printed in it, Unlike when i delete the line `for item in account.inbox.all().only('subject').order_by('-datetime_received')[:1]: print(item.subject)`, the code works fine, when any mail comes, its print "new email arrived" – bfs Sep 27 '22 at 15:52

0 Answers0