Search code examples
pythonoutlookwin32comoffice-automation

Reading Outlook emails from another address with win32com [using python]


I'm trying to read emails from outlook address using win32com, but I can only access the items from local outlook address. Do you know how to input the needed credentials for another address? In my Dispatch() method accepts username parameter but no password.

Here is my approach:

import win32com.client
        outlook = win32com.client.Dispatch("Outlook.Application", userName='[email protected]').GetNamespace("MAPI")
        for account in outlook.Session.Accounts:
        print (account.DisplayName) # [email protected]
        print (account.SmtpAddress)
                print (account.UserName)
                print(account.DeliveryStore.DisplayName)
                dir(win32com.client.Dispatch)
                help(win32com.client.Dispatch)
                print(outlook.Session.Accounts.GetIDsOfNames)

And my output from the console:

enter image description here

Thank you in advance!


Solution

  • You can use the NameSpace.Logon method which logs the user on to MAPI, obtaining a MAPI session. Use the Logon method only to log on to a specific profile when Outlook is not already running. This is because only one Outlook process can run at a time, and that Outlook process uses only one profile and supports only one MAPI session. When users start Outlook a second time, that instance of Outlook runs within the same Outlook process, does not create a new process, and uses the same profile.

    Also you may find the NameSpace.GetGlobalAddressList method helpful which returns an AddressList object that represents the Exchange Global Address List. GetGlobalAddressList supports only Exchange servers. It returns an error if the Global Address List is not available or cannot be found.

    You can use the NameSpace.Stores property which returns a Stores collection object that represents all the Store objects in the current profile. A profile defines one or more email accounts, and each email account is associated with a server of a specific type. For an Exchange server, a store can be on the server, in an Exchange Public folder, or in a local Personal Folders File (.pst) or Offline Folder File (.ost). For a POP3, IMAP, or HTTP email server, a store is a .pst file. Use the Stores and Store objects to enumerate all folders and search folders on all stores in the current session.

    If you need to get accounts not configured in Outlook, but shared ones. The NameSpace.GetSharedDefaultFolder method returns a Folder object that represents the specified default folder for the specified user. This method is used in a delegation scenario, where one user has delegated access to another user for one or more of their default folders (for example, their shared Calendar folder).