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.
# 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
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. |
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.