Python Outlook:读取额外邮箱的收件箱

7 投票
6 回答
19065 浏览
提问于 2025-04-18 01:50

我正在使用Outlook 2010,并且我的主邮箱是:name@company.com

我还在我的个人资料中添加了另一个邮箱:mb data proc

这两个邮箱在Outlook中都显示为顶级文件夹:

name@company.com
-Inbox
-Sent Items
-Deleted Items

mb data proc
-Inbox
-Sent Items
-Deleted Items

我无法为这个额外的邮箱创建一个不同的个人资料。它是添加在同一个个人资料中的。

我该如何获取“mb data proc”邮箱中的收件箱的引用呢?

这和这里描述的问题是一样的 获取额外收件箱的引用,不过这是用VBS写的。

那么用Python该怎么做呢?

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder=outlook.Folders("mb data proc")
msg=folder.Items
msgs=msg.GetLast()
print msgs    

我试过这个,但我得到了这个错误:

       folder=outlook.Folders("mb data proc")
AttributeError: _Folders instance has no __call__ method

6 个回答

0

感谢你的分享!

这是我根据你的建议整理出来的一个函数,用来读取可用的文件夹:

这是我第一次发帖,希望我把代码复制得正确:

def check_shared(namespace,recip = None): 
    """Function takes two arguments:
        .) Names-Space: e.g.: 
            which is set in the following way: outlook = Dispatch("Outlook.Application").GetNamespace("MAPI") and
        .) Recipient of an eventual shared account as string: e.g.: Shared e-Mail adress is "shared@shared.com"
            --> This is optional --> If not added, the standard-e-Mail is read out"""


    if recip is None:
        for i in range(1,100):                           
            try:
                inbox = namespace.GetDefaultFolder(i)     
                print ("%i %s" % (i,inbox))            
            except:
                #print ("%i does not work"%i)
                continue
    else:
        print('The folders from the following shared account will be printed: '+recip)
        tmpRecipient = outlook.CreateRecipient(recip)
        for i in range(1,100):                           
            try:
                inbox = namespace.GetSharedDefaultFolder(tmpRecipient, i)     
                print ("%i %s" % (i,inbox))            
            except:
                #print ("%i does not work"%i)
                continue
    print("Done")
2

我在尝试访问额外的邮箱,并从这些共享文件夹中读取收件箱的内容。

import win32com.client

>>> outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI").Folders

>>> folder = outlook(1)

>>> inbox = folder.Folders("Inbox")

>>> message = inbox.Items

>>> messages = message.GetLast()

>>> body_content = messages.body

>>> print (body_content)
2

如果你想在Outlook中查找其他邮箱或者你可以访问的独立PST文件,可以试试使用“Store / Stores MAPI对象”。

import win32com.client

for stor in win32com.client.Dispatch("Outlook.Application").Session.Stores:
    print( stor.DisplayName) 

顺便提一下,.Session 返回的内容和 .GetNamespace("MAPI") 是一样的。

参考链接:https://learn.microsoft.com/en-us/office/vba/api/overview/outlook

3

这里有个简单的解决办法。我觉得你唯一漏掉的就是进入 "mb data proc" 文件夹里的 "Inbox" 文件夹。

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders("mb data proc")
inbox = folder.Folders("Inbox")
msg = inbox.Items
msgs = msg.GetLast()
print msgs
5

我之前也有类似的疑问,按照我的理解,这里提到的解决方案是针对Python 2.7的。

我会尽量用简单的方式来解释如何在Python 3.+版本中操作。

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.Folders.Item("Mailbox Name")
inbox = folder.Folders.Item("Inbox")
msg = inbox.Items
msgs = msg.GetLast()
print (msgs)
print (msgs.Subject)

因为_Folder不能直接调用,所以在Python 3+中,你需要使用Folders.Item()这个方法来引用你的邮箱。

希望这对你有帮助。谢谢!

撰写回答