The New-MgUserMessage cmdlet in Microsoft Graph PowerShell enables administrators and users to create new email messages programmatically within Microsoft 365 mailboxes. This cmdlet allows the creation of simple or HTML-formatted emails, as well as messages with attachments, providing a powerful way to automate email communication.
The New-MgUserMessage cmdlet is useful for:
Before using New-MgUserMessage, you need to set up Microsoft Graph PowerShell and authenticate with the necessary permissions.
Install the Module
Run the following command to install the Microsoft Graph module:
Install-Module Microsoft.Graph -Scope CurrentUser
This installs the module for the current user without requiring administrative privileges.
Connect to Microsoft Graph
Authenticate and connect to Microsoft Graph with the required permissions:
Connect-MgGraph -Scopes "Mail.ReadWrite"
You'll be prompted to sign in using a Microsoft 365 account with the necessary permissions to create emails.
Disconnect After Use
Once you've completed your tasks, always disconnect the session:
Disconnect-MgGraph
This helps maintain security and prevent unnecessary active sessions.
The New-MgUserMessage cmdlet allows users to create new email messages, set their recipients, define their content, and add attachments. The flexibility of the cmdlet makes it a great tool for automating email workflows.
Cmdlet Syntax
New-MgUserMessage -UserId <String> -BodyParameter <IMicrosoftGraphMessage> [<CommonParameters>]
Create a Simple Email Message
$params = @{
subject = "Meeting Reminder"
body = @{
contentType = "Text"
content = "This is a reminder for the team meeting scheduled for tomorrow at 10 AM."
}
toRecipients = @(
@{
emailAddress = @{
address = "john.doe@example.com"
}
}
)
}
New-MgUserMessage -UserId "jane.doe@example.com" -BodyParameter $params
Create an HTML Email Message
$params = @{
subject = "Project Update"
body = @{
contentType = "HTML"
content = "<p>Dear Team</p><p>Please find the latest updates on the project.</p><p>Best
Regards<br/>Jane</p>"
}
toRecipients = @(
@{
emailAddress = @{
address = "team@example.com"
}
}
)
}
New-MgUserMessage -UserId "jane.doe@example.com" -BodyParameter $params
Create an Email with Attachments
$params = @{
subject = "Monthly Report"
body = @{
contentType = "Text"
content = "Please find the attached monthly report."
}
toRecipients = @(
@{
emailAddress = @{
address = "manager@example.com"
}
}
)
attachments = @(
@{
"@odata.type" = "#microsoft.graph.fileAttachment"
name = "report.pdf"
contentBytes =
[System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes("C:\Reports\report.pdf"))
}
)
}
New-MgUserMessage -UserId "jane.doe@example.com" -BodyParameter $params
To optimize the use of New-MgUserMessage, follow these best practices:
1. Can I create emails for multiple recipients using New-MgUserMessage?
Yes, you can include multiple recipients in the toRecipients array by adding additional email addresses.
2. How do I format an email using New-MgUserMessage with both plain text and HTML?
You can set contentType to HTML in the body parameter to format the email content using HTML tags.
3. Can I create emails for a shared mailbox using New-MgUserMessage?
Yes, but you need Send As or Send on Behalf permissions for the shared mailbox.
4. How do I add multiple attachments using New-MgUserMessage?
You can add multiple attachment objects in the attachments array following the same structure used for single attachments.
5. How do I troubleshoot permission-related errors?
Ensure that the user running the command has the Mail.ReadWrite API permission and that the token is properly authenticated.
Error: Access Denied
Cause: The user does not have Mail.ReadWrite permission.
Solution: Ensure that the user account has the
required API permission.
Error: Invalid Recipient Address
Cause: The email address provided is incorrect or does not exist.
Solution: Double-check the
recipient email format and ensure the user exists.
Error: Attachment Size Limit Exceeded
Cause The attachment exceeds the allowed size limit.
Solution: Compress the file or use an
alternative file-sharing method.
Error: Message Not Sent
Cause: API rate limit exceeded or misconfigured parameters.
Solution: Check API limits and ensure
the correct parameters are used.
The New-MgUserMessage cmdlet is a powerful tool for sending emails programmatically using Microsoft Graph PowerShell. By leveraging its capabilities, IT administrators and developers can automate email workflows and enhance communication within Microsoft 365 environments.
Follow the best practices outlined in this guide to make the most out of New-MgUserMessage while ensuring security and efficiency in your PowerShell scripts.
Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.
Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.
© Your Site Name. All Rights Reserved. Design by HTML Codex