2

I have used exchangelib library to download emails from my Inbox. The messages are eventually instance of exchangelib.items.Message. I want to save this entire email as .msg file so that I can later attach it to some the application. Can someone please let me know how I can do this in python ? In Below code I want to save each element of the msgs list. Currently I am working on just one email.

'''

from exchangelib import Account, Configuration, Credentials, DELEGATE

def connect(server, email, username, password):
    """
    Get Exchange account cconnection with server
    """
    creds = Credentials(username=username, password=password)
    config = Configuration(server=server, credentials=creds)
    return Account(primary_smtp_address=email, autodiscover=False, config = config, access_type=DELEGATE)

def get_recent_emails(account, folder_name, count):
    """
    Retrieve most emails for a given folder
    """
    # Get the folder object
    folder = account.inbox / folder_name
    # Get emails
    return folder.all().order_by('-datetime_received')[:count]

account = connect(server, email, username, password)

emails = get_recent_emails(account, 'BSS_IT', 1)
msgs = []
for msg in emails:
    msgs.append(msg)

'''

SJN
  • 377
  • 2
  • 8
  • 18
pbdscoder
  • 33
  • 4

1 Answers1

2

I'm not sure there's a recognized standard for the format of .eml files, but at least some email clients dump the raw MIME content, which is available in exchangelib as Message.mime_content.

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
  • Although it is not the complete answer, I am accepting it as I can use the .eml attachment in my task. But if anyone has solution to create .msg file then please post it. – pbdscoder Nov 18 '19 at 04:29