Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中保存使用exchangelib库生成的电子邮件消息项_Python_Email_Save_Msg_Exchangelib - Fatal编程技术网

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

如何在python中保存使用exchangelib库生成的电子邮件消息项,python,email,save,msg,exchangelib,Python,Email,Save,Msg,Exchangelib,我使用exchangelib库从收件箱下载电子邮件。 这些消息最终是exchangelib.items.Message的实例。 我想将整个电子邮件另存为.msg文件,以便以后可以将其附加到应用程序的某些部分。 有人能告诉我如何在python中做到这一点吗? 在下面的代码中,我想保存msgs列表的每个元素。目前我只处理一封电子邮件 ''' “”“我不确定.eml文件的格式是否有公认的标准,但至少有些电子邮件客户端转储原始MIME内容,这些内容在exchangelib中作为消息提供。MIME\u c

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

'''


“”“

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

什么是
类型(msg)
类型(msg)=顺便说一句,您可以使用
msgs=list简化最后4行(get_recent_emails(帐户'BSS_IT',1))
对于原始源(.eml)
msgs=[get_recent_emails(帐户'BSS_IT',1')中的m.mime_内容]
将更改它。虽然这不是完整的答案,但我接受它,因为我可以在任务中使用.eml附件。但如果有人有创建.msg文件的解决方案,请发布它。
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)