Sending emails programmatically in Microsoft 365 (M365) has become more accessible and efficient thanks to Graph PowerShell. One powerful cmdlet that helps with this task is Send-MgUserMessage. In this guide, we'll walk you through everything you need to know to start sending mails effortlessly.
Send-MgUserMessage is a Microsoft Graph PowerShell cmdlet that allows you to send an existing draft email from a user's mailbox in Microsoft 365. Instead of crafting and sending an email in one step, you first create a draft (using New-MgUserMessage) and then send it using Send-MgUserMessage.
This two-step process provides flexibility for scenarios where you might want to review or modify a message before dispatching it.
Send-MgUserMessage -UserId <String> -MessageId <String>
Parameters:
Note: The MessageId must reference a draft message. Otherwise, you will encounter an error.
# Create the draft message
$draftMessage = @{
subject = "Reminder: Upcoming Meeting"
body = @{
contentType = "HTML"
content = "This is a reminder for your upcoming meeting scheduled for tomorrow at 10 AM."
}
toRecipients = @(@{ emailAddress = @{ address = "jane.doe@domain.com" } })
}
# Create the message and retrieve the MessageId
$message = New-MgUserMessage -UserId "john.doe@domain.com" -BodyParameter $draftMessage
# Send the message
Send-MgUserMessage -UserId "john.doe@domain.com" -MessageId $message.Id
# Create the follow-up draft message
$followUpMessage = @{
subject = "Follow-Up: Action Required"
body = @{
contentType = "Text"
content = "Please follow up on the task assigned to you."
}
toRecipients = @(@{ emailAddress = @{ address = "team.member@domain.com" } })
}
# Create and send the follow-up message
$message = New-MgUserMessage -UserId "manager@domain.com" -BodyParameter $followUpMessage
Send-MgUserMessage -UserId "manager@domain.com" -MessageId $message.Id
# Read users from a CSV
$recipients = Import-Csv "recipients.csv"
foreach ($recipient in $recipients) {
# Create draft for each user
$notificationMessage = @{
subject = "Important Update"
body = @{
contentType = "Text"
content = "Please be informed about the recent updates to our policy."
}
toRecipients = @(@{ emailAddress = @{ address = $recipient.Email } })
}
# Create and send the message
$message = New-MgUserMessage -UserId "admin@domain.com" -BodyParameter $notificationMessage
Send-MgUserMessage -UserId "admin@domain.com" -MessageId $message.Id
}
Tip: Ensure your recipients.csv file has a header column named Email with user email addresses.
Send-MgUserMessage is a fantastic cmdlet that makes it easy to send Microsoft 365 emails through a secure, programmable method. Whether you're sending single emails, automatic follow-ups, or bulk notifications, this cmdlet ensures you maintain full control over your mail-sending processes.
Start experimenting today by combining New-MgUserMessage and Send-MgUserMessage, and unlock powerful automation opportunities for your M365 environment!
Need more help automating M365 tasks? Stay tuned for more Graph PowerShell tutorials!
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