Authenticate using Okta from a python program


I have a requirement - connect with the generic email account within corporate domain and get the attachment downloaded - I can easily do with my personal outlook account using a python package - but when I am trying to connect corporate email account - getting some connectivity issue as its always singing through Okta. Do you have any idea how to make the connectivity happen using Okta with corporate mail exchange server using IMAP4.

Here what I am trying to achieve is – authenticate the generic account from my Python program and then connect with the corporate mail exchange server.

The following code snippet working fine with personal account but getting some connectivity issue when trying connect with corporate mail server.

Appreciate your any advice and help.

import imaplib

import email

import os

svdir = ‘/Users/xxxxx/Desktop/Dwnld_email_attachment’

mail = imaplib.IMAP4_SSL(‘’,993)

mail.login(“”, “****”)“Inbox”)

typ, msgs =, ‘(SUBJECT “Test ASN File”)’)

msgs = msgs[0].split()

for emailid in msgs:

resp, data = mail.fetch(emailid, "(RFC822)")

email_body = data[0][1]

m = email.message_from_string(email_body)

if m.get_content_maintype() != 'multipart':


for part in m.walk():

    if part.get_content_maintype() == 'multipart':


    if part.get('Content-Disposition') is None:


    filename = part.get_filename()

    if filename is not None:

        sv_path = os.path.join(svdir, filename)

        if not os.path.isfile(sv_path):


            fp = open(sv_path, 'wb')