我使用 exchangelib 库从我的收件箱下载电子邮件。消息最终是 exchangelib.items.Message 的实例。我想将整封电子邮件保存为 .msg 文件,以便稍后将其附加到某些应用程序中。有人可以让我知道如何在 python 中做到这一点吗?在下面的代码中,我想保存 msgs 列表的每个元素。目前我只处理一封电子邮件。
'''
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)
'''