Home > Software engineering >  Want to send an Outlook email from Python with win32com.client from a other email address that defau
Want to send an Outlook email from Python with win32com.client from a other email address that defau

Time:01-06

I would like to be able to send a mail automatically with Python and win32com.client. Here is my code that works:

import win32com.client
o = win32com.client.Dispatch("Outlook.Application")
    
mail = o.CreateItem(0)
mail.To = "[email protected]"
    
mail.CC = "..@..."
    
mail.Subject = "subject"
mail.Body = "main body
  
mail.Send()

However, I would like to be able to send this mail from another of my Outlook mail addresses. So I would like to put in argument or option the sender of the mail.

I searched a lot on the internet but I didn't find anything.

CodePudding user response:

What about this (SentOnBehalfOfName)? There seems to be a difference of usage depending on the kind of the account. See here: Choosing "From" field using python win32com outlook

outlook = win32com.client.Dispatch("Outlook.Application")
mail = outlook.CreateItem(0)
mail.SentOnBehalfOfName = '[email protected]'
mail.To = '[email protected]'
mail.Subject = 'Hello'
mail.HTMLBody = template
  • Related