简体   繁体   English

如何在 python 中保存使用 exchangelib 库生成的 email 消息项

[英]How to save email messge item generated using exchangelib library in python

I have used exchangelib library to download emails from my Inbox.我使用 exchangelib 库从我的收件箱下载电子邮件。 The messages are eventually instance of exchangelib.items.Message.消息最终是 exchangelib.items.Message 的实例。 I want to save this entire email as.msg file so that I can later attach it to some the application.我想将整个 email 保存为.msg 文件,以便以后可以将其附加到某些应用程序中。 Can someone please let me know how I can do this in python?有人可以告诉我如何在 python 中做到这一点吗? In Below code I want to save each element of the msgs list.在下面的代码中,我想保存 msgs 列表的每个元素。 Currently I am working on just one email.目前我正在研究一个 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)

''' '''

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 .我不确定.eml文件的格式是否有公认的标准,但至少有一些 email 客户端会转储原始 MIME 内容,这些内容在 exchangelib 中作为Message.mime_content提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM