0

I have three mailboxes on my Outlook. How can I use my other mailbox in sending email via Python? The below script currently uses my main inbox = jgliban@ims.com. How can I use my other mailbox (jgliban@iqvia.com) in sending the email on the script?

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.GetInspector
mail.To = 'jgliban@ims.com'
mail.CC = 'jgliban@ims.com'
mail.Subject = 'Test Email'
index = mail.HTMLbody.find('>', mail.HTMLbody.find('<body')) 
mail.HTMLbody = mail.HTMLbody[:index + 1] + 'Hello,<br> <br>This is a test email<br><br>Many thanks and kind regards. ' + mail.HTMLbody[index + 1:]
mail.Send()
Janine
  • 37
  • 8
  • By changing `mail.To` value to `jgliban@iqvia.com` or whatever? – vahdet Feb 18 '19 at 08:56
  • The mail.To is the recipient of the Test Email. Above, if run in Python, will use my main inbox = jgliban@ims.com to send a Test Email to my main inbox = jgliban@ims.com. I want to use another inbox = jgliban@iqvia.com to send the above Test Email to my main inbox = jgliban@ims.com. – Janine Feb 18 '19 at 09:02
  • You mean your *account*, then I guess. As you expressed as *mailbox*, it sounds like the `To` address. In short, you want to change the `From` address, right? – vahdet Feb 18 '19 at 09:06
  • Yes, I wanted to change the From address. – Janine Feb 18 '19 at 09:08

2 Answers2

2

I was about to suggest SMTP, but since you are already connecting to the outlook application using win32, maybe this will help?

Need to switch accounts in outlook using python for sending email using other account

From the answer in the link:

Set MailItem.SendUsingAccount property.

Edit: Umm. It is given in the comments:

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
for acc in outlook.Session.Accounts:
    print (acc)
    if str(acc) == 'jgliban@iqvia.com':
        mail.SendUsingAccount = acc
javapyscript
  • 720
  • 8
  • 22
  • Hi. How do I go about doing that? Sorry. I'm practically new to Python. – Janine Feb 18 '19 at 11:08
  • Thank you. It's still sending using my other account though. I've just added the script you gave as is on my script. – Janine Feb 19 '19 at 00:39
2

If it is an Exchange mailbox, set the MailItem.SentOnBehalfOfName property to the name of the delegate mailbox.

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