Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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遍历辅助Outlook收件箱中的文件夹_Python_Outlook_Pywin32_Win32com - Fatal编程技术网

使用Python遍历辅助Outlook收件箱中的文件夹

使用Python遍历辅助Outlook收件箱中的文件夹,python,outlook,pywin32,win32com,Python,Outlook,Pywin32,Win32com,我最大的问题是,我在Outlook中的个人资料中附带了几个电子邮件帐户。他们都属于同一个领域作为我的主要之一。我可以发送和接收其他帐户。但是,使用下面的代码,我无法访问其他帐户的收件箱文件夹。只是我的默认文件夹,即mapi.GetDefaultFolder(6)。我如何访问其他帐户的收件箱并下载附件,因为这正是我需要的?我尝试了mapi.Folders('hhh@yyy.com“)。文件夹('收件箱')。项目,但这不起作用。你知道怎么解决这个问题吗 import win32com.client

我最大的问题是,我在Outlook中的个人资料中附带了几个电子邮件帐户。他们都属于同一个领域作为我的主要之一。我可以发送和接收其他帐户。但是,使用下面的代码,我无法访问其他帐户的收件箱文件夹。只是我的默认文件夹,即mapi.GetDefaultFolder(6)。我如何访问其他帐户的收件箱并下载附件,因为这正是我需要的?我尝试了
mapi.Folders('hhh@yyy.com“)。文件夹('收件箱')。项目
,但这不起作用。你知道怎么解决这个问题吗

import win32com.client
import pendulum

outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
Inbox = mapi.GetDefaultFolder(6) #Can be  replaced with Messages = mapi.Folders('xxx@yyy.com').Folders('Inbox').Items . If this  happens, then no need to define  Messages below.
Messages = Inbox.Items


received_date = pendulum.now().last(pendulum.MONDAY) #Search for emails since last Monday
received_date = received_date.strftime('%m/%d/%Y %H:%M %p') #Change  the date format
messages = Messages.Restrict("[ReceivedTime] >= '" + received_date + "'") #Restrict the received time i.e. > than or equal to last Monday
messages = Messages.Restrict("[Subject] = 'Automatic Reports'") #Title  contains   this 

outputDir = r"C:\Users\xxx\TestEmails"
try:
    for message in list(messages):
        try:
            s = message.sender
            for attachment in message.Attachments:
                attachment.SaveASFile(os.path.join(outputDir, attachment.FileName))
                print(f"attachment {attachment.FileName} from {s} saved")
        except Exception as e:
            print("error when saving the attachment:" + str(e))
except Exception as e:
    print("error when processing emails messages:" + str(e))

改用
Store
类的
GetDefaultFolder
方法。例如:

mapi = outlook.GetNamespace("MAPI")
Inbox = mapi.GetDefaultFolder(6)
这里使用
名称空间
类的
GetDefaultFolder
方法。但您真正需要的是迭代所有存储并使用以下方法:


谢谢你,尤金。不确定如何访问收件箱列表中的某个收件箱。访问收件箱中不是我的电子邮件默认文件夹的子文件夹如何?您可以使用获取根文件夹,然后递归遍历所有子文件夹,或者仅获取特定文件夹。
mapi = outlook.GetNamespace("MAPI")
for store in mapi.Stores: 
  Inbox = store.GetDefaultFolder(6)