This guide demonstrates how to use the New-MgUserMessage cmdlet in Microsoft Graph PowerShell to draft new emails. Learn how to compose messages, add recipients, and include attachments with practical examples
The New-MgUserMessage cmdlet is a powerful tool in the Microsoft Graph PowerShell module enabling administrators to create new messages in a user's mailbox. This article will delve into the cmdlet's syntax, provide usage examples, share tips, highlight use cases, and address possible errors and solutions.
New-MgUserMessage -UserId <String> -BodyParameter <IMicrosoftGraphMessage> [<CommonParameters>]
$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
$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
$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
#For Using New-MgUserMessage
Connect-MgGraph -Scopes “Mail.ReadWrite”
#For Using Send-MgUserMessage
Connect-MgGraph -Scopes “Mail.Send”
$draft = New-MgUserMessage -UserId "your.email@example.com" -BodyParameter $params
Send-MgUserMessage -UserId "your.email@example.com" -MessageId $draft.Id
This approach provides an opportunity to verify the email content before dispatching it.
try {
$draft = New-MgUserMessage -UserId "your.email@example.com" -BodyParameter $params
Send-MgUserMessage -UserId "your.email@example.com" -MessageId $draft.Id
}
catch {
Write-Error "An error occurred: $_"
}
This script attempts to send the email and catches any errors that occur during the process.
Cause: The authentication token provided is not valid or has expired.
Solution: Ensure you have a valid authentication token. Re-authenticate using the Connect-MgGraph cmdlet.
Connect-MgGraph -Scopes "Mail.Send"
Cause: The specified user ID or UPN does not exist.
Solution: Verify the user ID or UPN. Ensure it is correct and the user exists in your tenant.
Cause: The message body parameter is not correctly formatted.
Solution: Double-check the format of the -BodyParameter. Ensure all required fields are included and correctly structured.
1. What is New-MgUserMessage used for?
New-MgUserMessage is a Microsoft Graph PowerShell cmdlet used to create draft email messages in a user’s mailbox, with options to specify recipients, subject, body, and attachments.
2. How can I ensure the draft is saved in the user’s mailbox?
The draft is automatically saved when using New-MgUserMessage. You can verify it by listing messages in the Drafts folder:
Get-MgUserMailFolderMessage -UserId "<UserPrincipalName>" -MailFolderId "Drafts"
3. What permissions are required to draft messages?
You need the Mail.ReadWrite permission in Microsoft Graph PowerShell. Ensure appropriate permissions are granted in Azure AD.
4. How can I add multiple recipients to an email using New-MgUserMessage?
You can include multiple recipients by adding their email addresses to the toRecipients array within the -BodyParameter hashtable. For example:
$params = @{
subject = "Team Meeting"
body = @{
contentType = "Text"
content = "Reminder for the team meeting scheduled tomorrow at 10 AM."
}
toRecipients = @(
@{
emailAddress = @{
address = "john.doe@example.com"
}
},
@{
emailAddress = @{
address = "jane.smith@example.com"
}
}
)
}
New-MgUserMessage -UserId "your.email@example.com" -BodyParameter $params
This script drafts an email addressed to both John Doe and Jane Smith.
5. How do I specify the sender of the email when using New-MgUserMessage?
The sender is determined by the UserId parameter, which specifies the mailbox where the draft is created. To send an email from a shared mailbox or on behalf of another user, ensure you have the appropriate permissions (e.g., "Send As" or "Send on Behalf") and specify the shared mailbox's user ID or UPN in the UserId parameter.
The New-MgUserMessage cmdlet is a versatile tool for creating and sending email messages programmatically within a user's mailbox. By understanding its syntax, leveraging usage examples, and being aware of potential errors, administrators can effectively automate email communications, improve workflow efficiency, and enhance their Microsoft 365 environment management. Whether for automated notifications, report distributions, or team communications, New-MgUserMessage offers a robust solution for a variety of scenarios.
© m365corner.com. All Rights Reserved. Design by HTML Codex