如何在python中保存使用exchangelib库生成的电子邮件消息项目

我已经使用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,password)

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

'''

w5201021965 回答:如何在python中保存使用exchangelib库生成的电子邮件消息项目

我不确定.eml文件的格式是否存在公认的标准,但是至少某些电子邮件客户端转储了原始MIME内容,该内容在exchangelib中以Message.mime_content的形式提供。

本文链接:https://www.f2er.com/3138562.html

大家都在问