【问题标题】:Python Outlook: Read Inbox of additional mailboxPython Outlook:阅读附加邮箱的收件箱
【发布时间】:2014-05-20 21:06:55
【问题描述】:

我正在使用 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”邮箱中收件箱的引用?

这与此处Get reference to additional Inbox 描述的问题相同,但在 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

【问题讨论】:

    标签: python outlook inbox


    【解决方案1】:

    我也有类似的疑问,据我了解,此处所述的解决方案适用于 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() 方法来引用您的邮箱。

    希望对您有所帮助。谢谢!

    【讨论】:

      【解决方案2】:

      这是一个简单的解决方案。我认为您错过的唯一部分是进入"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
      

      【讨论】:

        【解决方案3】:

        我试图访问其他邮箱并从这些共享文件夹中读取收件箱

        导入 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)
        

        【讨论】:

          【解决方案4】:

          如果您在 Outlook 中寻找其他邮箱或您可以访问的单独 PST 文件,请尝试使用 Store / Stores MAPI 对象。

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

          PS .Session 返回与 .GetNamespace("MAPI") 相同的引用

          供参考https://docs.microsoft.com/en-us/office/vba/api/overview/outlook

          【讨论】:

            【解决方案5】:

            感谢您的帖子! 这是我根据您的输入汇总的一个函数,用于读出可用的文件夹:

            这是我的第一篇文章,所以我希望我正确地复制了代码:

            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")
            

            【讨论】:

              【解决方案6】:

              首先,您可以使用 Namespace.GetSharedDefaultFolder 方法。

              其次,再行

              folder=outlook.Folders("mb data proc")

              需要

              folder=outlook.Folders.Item("mb data proc")

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2013-09-24
                • 2011-06-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-10-27
                相关资源
                最近更新 更多