Sending Emails on Behalf of Another User with Graph PowerShell

In many organizations, administrators often need to send emails on behalf of shared mailboxes, managers, or other users. Whether it’s a shared mailbox handling team notifications or an assistant sending emails for an executive, automating this process can save time and streamline workflows.

Using Microsoft Graph PowerShell, you can send emails on behalf of any user, provided the necessary permissions are in place. This article provides a simple yet powerful script to accomplish this task, along with insights into how it works and solutions for common errors.

The Script: Send an Email on Behalf of Another User

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Mail.Send"

# Define the sender (on behalf of whom the email is sent) and the recipient
$SenderId = "sharedmailbox@yourdomain.com"
$RecipientEmail = "recipient@domain.com"

# Define the email content
$emailMessage = @{
    Subject = "Important Update"
    Body = @{
        ContentType = "HTML"
        Content = "Dear Recipient,<br><br>This email is sent on behalf of the shared mailbox.<br><br>Regards,<br>Admin"
    }
    ToRecipients = @(@{ EmailAddress = @{ Address = $RecipientEmail } })
}

# Send the email on behalf of the sender
New-MgUserMessage -UserId $SenderId -BodyParameter $emailMessage -Send

Write-Host "Email sent on behalf of $SenderId to $RecipientEmail."

# Disconnect from Microsoft Graph
Disconnect-MgGraph

How The Script Works

  • Connect to Microsoft Graph: The script connects to Microsoft Graph with the Mail.Send permission, allowing the script to send emails.
  • Define Sender and Recipient: Specify the email address of the sender (shared mailbox or user) and the recipient.
  • Compose the Email: The script uses a hashtable to define the subject, body (HTML), and recipient details.
  • Send the Email: The New-MgUserMessage cmdlet sends the email immediately on behalf of the sender.
  • Disconnect: The session with Microsoft Graph is terminated after the operation.

Possible Errors and Solutions

Error Cause Solution
Access Denied The account running the script doesn’t have sufficient permissions. Ensure the account has Mail.Send permission and delegation rights (Send As or Send on Behalf).
Invalid Sender ID The sender email address or ID is incorrect. Verify the sender’s email address and ensure the script uses a valid User ID.
Microsoft Graph Connection Failure Token expiration or incorrect scopes. Reconnect to Microsoft Graph with valid scopes using Connect-MgGraph.
Email Not Sent Invalid recipient email address or missing message body. Ensure recipient details and message content are defined correctly in the script.

Conclusion

Sending emails on behalf of another user is a common task for administrators, especially in environments with shared mailboxes or delegated responsibilities. By leveraging Microsoft Graph PowerShell, you can automate this process, saving time and ensuring seamless communication across the organization.

© M365Corner. All Rights Reserved.