Showing posts with label Send outlook mail from non primary account using python. Show all posts
Showing posts with label Send outlook mail from non primary account using python. Show all posts

Wednesday, September 15, 2021

Send outlook mail from non primary account using python

 import win32com.client as client
import datetime
outlook=client.Dispatch("Outlook.Application")
message=outlook.Createitem(0)
namespace=outlook.GetNameSpace('MAPI')
inbox=namespace.GetDefaultFolder(6)
message=inbox.items.add
message.To="allsourav@gmail.com;allsourav2@gmail.com"
message.CC="souravandamiya@gmail.com"
message.BCC="allsourav2@gmail.com"
From = None
for myEmailAddress in outlook.Session.Accounts:
    
    if str(myEmailAddress)=="allsourav@gmail.com":
        From = myEmailAddress
        break

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

##message.SentOnBehalfOfName='"Sourav IT Faculty" <allsourav@gmail.com>'
##this method does not work with new version of outlook,it is for sending
##mail from non primary account set up in outlook

   
message.Subject="Happy Birthday on 14.09.2021"
message.Body="Wish you a happy birthday ,\n learning outlook using python again"
message.Importance=2
message.ReadReceiptRequested=True
message.OriginatorDeliveryReportRequested=True
#message.DeferredDeliveryTime="15/09/2021 01:31:05 AM"
#cSendDate=datetime.date.today() #todays date
#cSendDate=(datetime.date.today() + datetime.timedelta (days=1)).strftime("%d-%m-%y") #tomorrows date
#cSendDate=datetime.datetime.now() + datetime.timedelta(days=1) #1 day from current datetime
#cSendDate=datetime.datetime.now() + datetime.timedelta(minutes=2) #2 minutes from now
#cSendDate=cSendDate.strftime("%d/%m/%Y %H:%M:%S %p") #formats the date like the outlook wants it in "15/09/2021 01:31:05 AM" in this format
#print(cSendDate)
#message.DeferredDeliveryTime=cSendDate

message.Save()
message.Display()
message.Send()