0

I am pretty new to python and I am trying to do the following:

Use a python script to list the last 10 emails in a specific folder on exchange, however I keep getting the the following error

exchangelib.errors.ErrorNonExistentMailbox: No mailbox with such guid

Below is the script I am trying to run:

#!/usr/bin/env python
#coding:utf-8

from datetime import timedelta
from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, EWSDateTime, EWSTimeZone, Configuration, CalendarItem, Message, Mailbox, Attendee, Q, ExtendedProperty, FileAttachment, ItemAttachment, HTMLBody, Build, Version
from exchangelib import Configuration, GSSAPI, SSPI
from exchangelib.util import PrettyXmlHandler
from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter
import logging
import requests

logging.basicConfig(level=logging.DEBUG, handlers=[PrettyXmlHandler()])

def connect_exchange(account):
    exCredentials = Credentials(username='User@Domainname.com', password='**********')
    exConfig = Configuration(server='mail.domainname.com', credentials=exCredentials)
    account = Account(primary_smtp_address='User@Domainname.com', credentials=exCredentials, config=exConfig, autodiscover=False, access_type=DELEGATE)

    BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter

# Print first 100 inbox messages in reverse order
    for item in account.inbox.all().order_by('-datetime_received')[:10]:
        print(item.subject, item.body, item.attachments)
connect_exchange()

I can see that I am able to connect to the mail server, however when the script attempts the for loop appears to be when the error above gets thrown.

Has anyone encountered such an error before? If so so ids there any workarounds?

Thanks

Dunner1991
  • 19
  • 3

2 Answers2

0

This is the server telling you that it doesn't know a mailbox with that name. Maybe you're connecting to the wrong server, or you misspelled the email address, or the email address is an alias for the real mailbox?

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
0

I tested your snippet on my machine, and with minimal modifications it working for me just fine. Please check your set up credentials for any typos in them.

BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter
logging.basicConfig(level=logging.DEBUG, handlers=[PrettyXmlHandler()])


def connect_exchange():
    exCredentials = Credentials(username='admin@testexchange.local', password='Password')
    exConfig = Configuration(server='testexchange.local', credentials=exCredentials)
    account = Account(primary_smtp_address='user@testexchange.local', credentials=exCredentials, config=exConfig, autodiscover=False, access_type=DELEGATE)

    # Print first 100 inbox messages in reverse order
    for item in account.inbox.all().order_by('-datetime_received')[:10]:
        print(item.subject, item.body, item.attachments)


connect_exchange()
Dharman
  • 30,962
  • 25
  • 85
  • 135
danielbene
  • 25
  • 6