Search code examples
powershelloutlookexchange-serveroffice-automationsend-on-behalf-of

Powershell: Change sender name in outlook com object


I want to send out emails using Powershell and outlook but the sender email needs to be different. Currently this is what i have:

$OL = New-Object -comObject Outlook.Application
$mItem = $OL.CreateItem("olMailItem")

$mItem.SentOnBehalfOfName = "[email protected]"
$mItem.To = "[email protected]"
$mItem.Subject = "test"
$mItem.HTMLBody = "test"
$mItem.send()

My IT admin gave me an additional outlook email address. So i added the line for SentOnBehalfOfName but i get the error mail: "This message could not be sent. You do not have the permission to send the message on behalf of the specified user. "

Does anyone know what permissions are needed? I had an idea for a solution but I don't if it would work. My outlook has my default profile address. [email protected] and its the primary one somewhere in the system so when i make an outlook email through powershell , it uses it. Is there a way to switch to the new one i was given? If the new one is a shared email even though i am the only one it, will it still work?

Thank you

EDIT: Note that doing it directly from outlook by changing the From option does work for me. So now i just need to do it by powershell. I also tried setting the $mItem.Sender but i get the same error.


Solution

  • You don't have permissions to send on behalf of another person.

    In Exchange Server, you can use the Exchange admin center (EAC) or the Exchange Management Shell to assign permissions to a mailbox or group so that other users can access the mailbox (the Full Access permission), or send email messages that appear to come from the mailbox or group (the Send As or Send on Behalf permissions). The users that are assigned these permissions on other mailboxes or groups are called delegates. Read more about that in the Manage permissions for recipients article.

    The Grant Send on Behalf Permissions using Powershell page shows the code which can be used to set up permissions programmatically.

    Note, instead you may consider configuring another account in Outlook. In that case you don't need to have special permissions to send an email. The MailItem.SendUsingAccount property allows to set an Account object that represents the account under which the MailItem is to be sent.