I have a question regarding saving email data in batches using exchangelib. Currently it is taking a lot of time if there are many emails. After a few minutes it throws this error:
ERROR: MemoryError:
Retry: 0
Waited: 10
Timeout: 120
Session: 25999
Thread: 28148
Auth type: <requests.auth.HTTPBasicAuth object at 0x1FBFF1F0>
URL: https://outlook.office365.com/EWS/Exchange.asmx
HTTP adapter: <requests.adapters.HTTPAdapter object at 0x1792CE68>
Allow redirects: False
Streaming: False
Response time: 411.93799999996554
Status code: 503
Request headers: {'X-AnchorMailbox': 'myworkemail@workdomain.com'}
Response headers: {}
Here is the code that I use for connecting and reading:
def connect_mail():
config = Configuration(
server="outlook.office365.com",
credentials=Credentials(
username="myworkemail@workdomain.com", password="*******"
),
)
return Account(
primary_smtp_address="myworkemail@workdomain.com",
config=config,
access_type=DELEGATE,
)
def import_email(account):
tz = EWSTimeZone.localzone()
start = EWSDateTime(2020, 10, 26, 22, 15, tzinfo=tz)
for item in account.inbox.filter(
datetime_received__gt=start, is_read=False
).order_by("-datetime_received"):
email_body = item.body
email_subject = item.subject
soup = bs(email_body, "html.parser")
tables = soup.find_all("table")
item.is_read = True
item.save()
# Some code here for saving the email to a database