Send mail from another account - israel-dryer/Outlook-Python-Tutorial GitHub Wiki
If you have multiple accounts hooked up to your Outlook application, you may want to send from one or more of those accounts.
There is a property on the MailItem
called SendUsingAccount
. Unfortunately, this property doesn't appear to work in Python from my own experience and based on research I've done on the internet. However, there is a solution.... and here it is.
The example below assumes that my default profile is: [email protected]. However, I'd like to send from another account: [email protected], which is setup in my Outlook application.
import win32com.client as client
# dispatch the Outlook application
outlook = client.Dispatch('Outlook.Application')
# get the account object associated with the account you want to send from
gmail = outlook.Session.Accounts['[email protected]']
# create a mail item
mail_item = outlook.CreateItem(0)
mail_item.To = '[email protected]'
mail_item.Subject = 'Testing a message'
mail_item.Body = 'Hey there. I hope you get this message.'
# set the account to send from
mail_item._oleobj_.Invoke(*(64209, 0, 8, 0, gmail))
# send or save the message
mail_item.Send()
An alternative method to get the account object is by index (my gmail is in position 2)
# index with zero-based indexing
gmail = outlook.Session.Accounts[1]
# index with the `Item` property
gmail = outlook.Session.Accounts.Item(2)
# print a list of all accounts available
for account in outlook.Session.Accounts:
print(account)