0

My secondary outlook account does not appear in the list given by iterating over outlook.application CDispatch Session.Accounts.

So, I'm using 'SentOnBehalfOfName' instead, as suggested by https://stackoverflow.com/a/57324426/6057650 to sent emails.

And it works, but not at all or completely as I expected, since sent emails are stored in the primary account outbox and not in the secondary account outbox.

import win32com.client as win32

outlook = win32.Dispatch('outlook.application')

# see available mail accounts (no condition, just as info)
for accounts in outlook.Session.Accounts:
    print('Available email accounts: %s'%(accounts))

# Create the email to be sent
mail = outlook.CreateItem(0)
mail.SendUsingAccount='secondary_account@email.com'
mail._oleobj_.Invoke(*(64209, 0, 8, 0, 'secondary_account@email.com'))
mail.SentOnBehalfOfName = 'secondary_account@email.com'
mail.To = 'some_address@email.com'
mail.Subject = 'Report'
mail.Body ='Some stuff'

# getting default folder of used Account
myNamespace = outlook.GetNamespace("MAPI")
myFolder = myNamespace.GetDefaultFolder(6)
myFolder.Display()

# see your prepared email
mail.Display()

# Finally
mail.Send()

What I want (and need) is that sent emails be stored in the secondary account outbox (which is shared with other peers and which can be access by them) and not in the main or primary account outbox (which only I can access).

0m3r
  • 12,286
  • 15
  • 35
  • 71
Khristhian
  • 26
  • 5

1 Answers1

2

try the following

import win32com.client

Outlook = win32com.client.Dispatch("Outlook.Application")

for account in Outlook.Session.Accounts:
    if account.DisplayName == "secondary_account@email.com":
        print(account)
        email = Outlook.CreateItem(0)
        email._oleobj_.Invoke(*(64209, 0, 8, 0, account))
        email.Display()
0m3r
  • 12,286
  • 15
  • 35
  • 71
  • Hi 0m3r, suggested solution does not work because my secondary email account linked to my main email account is not listed in "Outlook.Session.Accounts" only the main email account is in there. The other two associated email accounts does not. So "email._oleobj_.Invoke" will not be executed. In my Outlook app I can see those linked mail accounts and use them, why can not see them with python win32client? – Khristhian Oct 01 '19 at 15:20
  • 1
    @0m3r's solution helped me to send from a secondary Microsoft Exchange account on a work laptop, tightly locked behind corporate group policy. `SentOnBehalfOfName` and `SendUsingAccount` didn't work in my case. Really appreciate your help, @0m3r! – llinfeng Apr 13 '23 at 11:46