Python Exchangelib:将电子邮件从收件箱移动到文件夹
我想读取收件箱中最新的电子邮件,从中选择附件,然后将电子邮件移动到文件夹中。我已经有了保存附件的代码:Python Exchangelib:将电子邮件从收件箱移动到文件夹,python,exchangelib,Python,Exchangelib,我想读取收件箱中最新的电子邮件,从中选择附件,然后将电子邮件移动到文件夹中。我已经有了保存附件的代码: from exchangelib import Credentials, Account import os credentials = Credentials('test.name@mail.com', 'password') account = Account('test.name@mail.com', credentials=credentials, autodiscover=True
from exchangelib import Credentials, Account
import os
credentials = Credentials('test.name@mail.com', 'password')
account = Account('test.name@mail.com', credentials=credentials, autodiscover=True)
for item in account.inbox.all().order_by('-datetime_received')[:1]:
for attachment in item.attachments:
fpath = os.path.join("C:/destination/path", attachment.name)
with open(fpath, 'wb') as f:
f.write(attachment.content)
但我无法将电子邮件移动到收件箱以外的其他文件夹。
到目前为止,我只找到了这个选项:
item.move(to_folder)
但我不知道该怎么写文件夹的名字。
有人能给我举个例子吗
提前感谢。指向
.move()
的to_folder
参数必须是文件夹
实例,而不是文件夹名称。下面是一个例子:
from exchangelib import Credentials, Account
import os
credentials = Credentials('test.name@mail.com', 'password')
account = Account('test.name@mail.com', credentials=credentials,
autodiscover=True)
#this will show you the account folder tree
print(account.root.tree())
#if to_folder is a sub folder of inbox
to_folder = account.inbox / 'sub_folder_name'
#if folder is outside of inbox
to_folder = account.root / 'folder_name'
for item in account.inbox.all().order_by('-datetime_received')[:1]:
for attachment in item.attachments:
fpath = os.path.join("C:/destination/path", attachment.name)
with open(fpath, 'wb') as f:
f.write(attachment.content)
item.move(to_folder)
感谢到目前为止的帮助,但现在我得到了一个带有“UnauthenticatedRequest”的案例错误,当我使用新代码时,甚至使用了与周五工作的代码完全相同的代码。你知道如何解决这个问题吗?