Python:将所有Outlook电子邮件移至其他文件夹
我正在研究一个项目,涉及从收到的电子邮件中删除数据。
我坚持的一部分是,将消息带入本地记忆后,我正在尝试将电子邮件从原始位置移动到档案文件夹是因为此过程将每周运行,我想确保事情不会倍增。
我正在使用一种非常标准的方法将电子邮件加载到内存中,并有点警告,这些电子邮件将进入一个单独的电子邮件组,而不是我的本机电子邮件帐户,这增加了一步。看起来这样:
outlook = win32com.client.Dispatch('Outlook.Application').GetNamespace('MAPI')
folder =outlook.Folders.Item('Product Alerts')
tempBox = folder.Folders.Item('Inbox')
inbox = tempBox.Folders.Item('Electronic Product Alert')
archiveFolder = folder.Folders.Item('Product Alert Archive')
messages = inbox.Items
#this is the part that isn't working
for email in inbox.Items:
email.Move(archiveFolder)
问题是将电子邮件移至存档文件夹的循环不会移动文件夹中的所有电子邮件,它会移动一些并留下其他电子邮件。
我也尝试过
for message in messages:
message.Move(archiveFolder)
,并且它与inbox.items相同,
我还检查了以确保消息正确加载,以确保与Outlook通信没有问题,但是运行
print(len(messages))
为电子邮件数量提供了正确的结果在文件夹中。
我是否缺少一些明显的东西,因为它阻止了该循环将所有电子邮件移至存档文件夹,还是需要使用其他方法?
I'm working on a project that involves scraping data from emails that come in.
The part I'm stuck on is, after I bring the messages into local memory, I'm trying to move the emails from the original location to an archive folder because this process will be ran weekly and I want to ensure things aren't getting double counted.
I'm using a pretty standard method to load the emails into memory, with the slight caveat that the emails are coming into a separate email group and not my native email account, which adds a step. It looks like this:
outlook = win32com.client.Dispatch('Outlook.Application').GetNamespace('MAPI')
folder =outlook.Folders.Item('Product Alerts')
tempBox = folder.Folders.Item('Inbox')
inbox = tempBox.Folders.Item('Electronic Product Alert')
archiveFolder = folder.Folders.Item('Product Alert Archive')
messages = inbox.Items
#this is the part that isn't working
for email in inbox.Items:
email.Move(archiveFolder)
The issue is that the loop that moves the emails to the archive folder doesn't move all the emails in the folder it moves some and leaves others.
I have also tried
for message in messages:
message.Move(archiveFolder)
and it does the same thing as inbox.Items
I also checked to make sure the messages were loading properly, to make sure there isn't an issue communicating with Outlook, but running
print(len(messages))
gives the correct result for the number of emails in the folder.
Am I missing something obvious that is stopping this loop from moving all of the emails to the archive folder or do I need to use a different method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我建议使用获取收件箱文件夹的方法,然后与子文件夹打交道。
可以更换此通话,因为根据用户的语言环境,名称可能会有所不同。
在以下代码中,您不会将项目移动并减少收集的帐户,因此每次都少一个项目:
相反,您需要使用代码中的循环进行反向以从代码中移动或删除项目收藏:
First of all, I'd recommend using the GetDefaultFolder method to get the Inbox folder and only then deal with subfolders.
This call can be replaced because depending on the user's locale the name can be different.
In the following code you don't take into an account that items are moved and the collection is decreased, so each time by one item less:
Instead, you need to use a reverse for loop in the code to move or delete items from the collection:
您的代码正在修改集合(通过调用
移动
)在您循环到其元素时。使用
message.count
向下的下循环。Your code is modifying the collection (by calling
Move
) while you are looping over its elements.Use a down loop from
messages.Count
down to 1.