1

I have the following code

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'madanraj.c@sss.com;
mail.Subject = 'Daily Backlog'
mail.Send()

I have two accounts in outlook and i installed redemption too, but struck how to switch account

i saw vb code and i cant convert to python code

set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Accounts = Session.Accounts
for each Account in Accounts
  Debug.Print Account.Name
next
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
Madan Raj
  • 279
  • 4
  • 15

3 Answers3

1

Set MailItem.SendUsingAccount property.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

I know that this comes late but I think people might find it useful. This is how I've managed to select an e-mail address to send from, as I had multiple addresses in outlook.

import win32com.client as win32

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

mail = outlook.CreateItem(0)
mail.Subject = "Test subject"
mail.To = "yourrecipient@gmail.com"

# If you want to set which address the e-mail is sent from. 
# The e-mail needs to be part of your outlook account.
From = None
for myEmailAddress in outlook.Session.Accounts:
    if "gmail.com" in str(myEmailAddress):
        From = myEmailAddress
        break

if From != None:
    # This line basically calls the "mail.SendUsingAccount = xyz@email.com" outlook VBA command
    mail._oleobj_.Invoke(*(64209, 0, 8, 0, From))

mail.Send()
CaptainCsaba
  • 266
  • 2
  • 12
0

mail.oleobj.Invoke(*(64209, 0, 8, 0, From)) - This command threw the below exception :-

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The item has been moved or deleted.', None, 0, -2147221238), None)

Please look into it

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 23 '22 at 21:08
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32521668) – Nefariis Aug 24 '22 at 15:17