Hello @Mahesh Jaiswal ,
The main difference betwen the "classic" way (through a SMTP server in the Domain) and O365 is that the Domain SMTP would allow running with the script run credentials, while O365 will require authentication for each run.
For example: if you add the variable "$Cred = Get-Credential" will prompt for the O365 mailbox credentials and your script should run smoothly this time.
There are 2 methods to allow applications or services to
AUTH Submission
DirectSend
Both are very well explained and implementation detailed in this article:
https://learn.microsoft.com/en-us/exchange/mail-flow-best-practices/how-to-set-up-a-multifunction-device-or-application-to-send-email-using-microsoft-365-or-office-365
Unless this is a vital service for a full application of service that is based upon SMTP through O365 to work, I would recommend to build up a simple SMTP server in the domain, for the purpose, as it should be faster to deploy and manage.
Hope this help you!
Best regards,