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

标签 python email save msg exchangelib

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

'''

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)

'''

最佳答案

我不确定 .eml 文件的格式是否有公认的标准,但至少有一些电子邮件客户端会转储原始 MIME 内容,这些内容在 Exchangelib 中作为 Message 提供。 mime_content

关于python - 如何在Python中保存使用exchangelib库生成的电子邮件消息项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58765068/

相关文章:

python - scrapy 无法在我的 Mac 上加载 libxslt.1.dylib

python - 以元组为键解析字典

associations - ExtJS 4 关联和 store.save()

C# 保存到文本文件中的多行

vb.net - 为 txt 保存设置正确的文件路径 - vb.NET

python - numpy 插入轴使数据不连续

python - 打印列表中的项目数

html - 在电子邮件开发中使用缩小的 HTML

html - 在 Outlook 中查看时,表格的右边框未显示在电子邮件模板中

python - 在 python 中编写带有指定回复的 Thunderbird