r/PowerShell • u/FerrousBueller • May 07 '26
Solved Send-MgUserMail multiple cc recipients
EDIT: solution found, corrected code at the bottom of the post.
I'm struggling with this one and could use some review/help.
We've migrated to exchange online so I'm updating our various scripts to send through it.
The only issue I'm having so far is figuring out to add multiple email address to the cc field using MgGraph / Send-MgUserMail.
When Exchange was on-premise we'd send the email with something like this to handle the multiple addresses:
$To = "$($helpDeskEmail)"
$Cc = "$($managerEmail)", "$($hrEmail)"
Send-MailMessage -From $From -To $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SmtpServerI have the app registration and certificate in place and can send through exchange online when there's a single address in the ccRecipients section and I change the ccRecipient address, it'll send to all the addresses in question separately without issues:
$emailParams = @{
message = @{
subject = "Testing New Hire Notification"
body = @{
contentType = "Text"
content =
"Testing new hire notification through exchange online - ignore for now"
}
toRecipients = @(
@{
emailAddress = @{
address = $helpDeskEmail
}
}
)
ccRecipients = @(
@{
emailAddress = @{
address = $managerEmail
}
}
)
}
saveToSentItems = "false"
}
Connect-MgGraph -NoWelcome -ClientId $ClientId -TenantId $TenantId -CertificateThumbprint $CertThumbprint
Send-MgUserMail -UserId "notification@contoso.com" -BodyParameter $emailParams
Disconnect-MgGraphI did find an article about outputting the recipients to a csv https://o365reports.com/how-to-send-emails-using-microsoft-graph-powershell/#Send%20an%20email%20to%20multiple%20recipients%20using%20CSV
I've set that up and manually created the csv for testing.
$ccAddresses = Import-Csv -Path "C:\scripts\NewUsers\temp\ccAddresses.csv"
$ccAddressList = @()
foreach ($ccAddress in $ccAddresses) {
$ccAddressList += @{
emailAddress = @{
address = $ccAddress.EmailAddress
}
}
}
$EmailParams = @{
message = @{
subject = "Testing New User Email"
body = @{
contentType = "Text"
content =
"Testing new user email notification - ignore for now"
}
toRecipients = @(
@{
emailAddress = @{
address = $helpDeskEmail
}
}
)
ccRecipients = @(
@{
emailAddress = @{
address = $ccAddressList
}
}
)
}
saveToSentItems = "false"
}
Connect-MgGraph -NoWelcome -ClientId $ClientId -TenantId $TenantId -CertificateThumbprint $CertThumbprint
Send-MgUserMail -UserId "notification@contoso.com" -BodyParameter $EmailParams
Disconnect-MgGraphRunning that returns invalid recipient
Send-MgUserMail : At least one recipient is not valid., Recipient 'System.Object[]' is not resolved. All recipients must be resolved before a message can be submitted.
Status: 400 (BadRequest)
ErrorCode: ErrorInvalidRecipientsIt'll return the same Invalid Recipients message if I try variations of
address = "$($managerEmail)", "$($hrEmail)"I'm not sure what I'm missing here, if anyone's got working code to share or can help me adjust this, I'd really appreciate a point in the right direction. There doesn't seem to be many examples of this that I can find.
FIXED Corrected code below
$ccAddresses = Import-Csv -Path "C:\scripts\NewUsers\temp\ccAddresses.csv"
$ccAddressList = foreach ($ccAddress in $ccAddresses) {
@{
EmailAddress = @{
Address = $ccAddress.EmailAddress
}
}
}
$EmailParams = @{
message = @{
subject = "Testing New User Email"
body = @{
contentType = "Text"
content = "Testing new user email notification - ignore for now"
}
toRecipients = @(
@{
emailAddress = @{
address = $helpDeskEmail
}
}
)
ccRecipients = @(
$ccAddressList
)
}
saveToSentItems = "false"
}
Connect-MgGraph -NoWelcome -ClientId $ClientId -TenantId $TenantId -CertificateThumbprint $CertThumbprint
Send-MgUserMail -UserId "notification@contoso.com" -BodyParameter $EmailParams
Disconnect-MgGraph
2
u/PinchesTheCrab May 07 '26 edited May 08 '26
Try converting $EmailParams to json using
$EmailParams | convertto-jsonand I think you'll see the issue pretty quickly.The ccRecipients field expects an array hashtables with an emailAddress property that is expected to be a [string]. You're creating a single emailAddress property with an array of hashtables as the emailAddress.
Does this work?
2
u/vermyx May 07 '26 edited May 08 '26
Try the following. You're incorrectly setting up the CC. You can set up any email list as the foreach loop setting up the $ccAdressList (whether it is one or 100) and then do $EmailParams.Message.ccRecipients = @($ccAdressList) as your set for your To,CC, or BCC list.
2
u/Unlikely_Tie1172 MVP, Community Blogger May 08 '26
There's a function called Populate-MessageRecipient that takes an array of SMTP addresses and formats them correctly for the Send-MgUserMail cmdlet in https://practical365.com/send-email-powershell-graph/. It also works with the Graph HTTP request.
3
u/ZercTastic May 07 '26
I don’t have a sample to give, but you need a “,” (comma) after each for the CC, aside from the last one. Might need to do a count of the CC addresses and have it out a comma on all but the last entry.
1
u/MadBoyEvo May 08 '26
This is quite a drama what official Graph cmdlets offer :-)
Please use mailozaurr:
This is the old blog showing how to use it for SMTP, Graph, etc
You can also use with
Connect-MgGraphas authentication mechanism, as it's my preference anyways nowadays.1
u/AdeelAutomates May 08 '26
Here is my function I built for Mail that simplifies the whole thing.
All you enter is values like so for it. the recipients are arrays not nested things and you can attach files with just a path:
The function:
0
u/[deleted] May 07 '26
[deleted] — view deleted comment
0
u/[deleted] May 07 '26
[deleted] — view deleted comment