使用 Microsoft Graph Powershell 向 excel .csv 电子表格中的多个收件人发送批量电子邮件

问题描述 投票:0回答:1

解决了。下面的代码有效。

我在使用以下使用 Microsoft Graph 发送电子邮件的 PowerShell 脚本向 Excel 电子表格中列出的多个收件人发送电子邮件时遇到问题。

当通过 PowerShell 终端作为“.\MyScript.ps1”运行脚本时,它会执行两次(乘以收件人数量),如果 .csv 文件中有三个收件人,则脚本会运行三次 - 并且同一电子邮件仅发送给列表中的第一个收件人 x 收件人数量。 (多次)。

触发行为的多个收件人列表示例

以.ps1文件形式使用的代码

# Import user list and information from .CSV file
$Users = Import-Csv UserList.csv

# Email details
Foreach ($User in $Users) {
$sender = "[email protected]"
$recipient = $User.Email
$subject = "Test Email"
$type = "HTML"
$save = "true"

$body = @"
Receiving email went through.
"@

# Import MgGraph module
Import-Module Microsoft.Graph.Users.Actions

# Connect to MgGraph with required permissions
Connect-MgGraph -Scopes 'Mail.Send', 'Mail.Send.Shared'

$params = @{
    Message         = @{
        Subject       = $subject
        Body          = @{
            ContentType = $type
            Content     = $body
        }
        ToRecipients  = @(
            @{
                EmailAddress = @{
                    Address = $recipient
                }
            }
        )
    }
    SaveToSentItems = $save
}
}
# Send message
Send-MgUserMail -UserId $sender -BodyParameter $params

任何有关如何解决此问题的建议将不胜感激。同样,只有在向 .csv 文件添加多个电子邮件收件人时才会遇到该错误,但在仅添加一个收件人时它可以正常工作。

提前谢谢您。

powershell microsoft-graph-api exchange-server azure-powershell
1个回答
0
投票

解决了。下面的代码有效。

我在使用以下使用 Microsoft Graph 发送电子邮件的 PowerShell 脚本向 Excel 电子表格中列出的多个收件人发送电子邮件时遇到问题。

当通过 PowerShell 终端作为“.\MyScript.ps1”运行脚本时,它会执行两次(乘以收件人数量),如果 .csv 文件中有三个收件人,则脚本会运行三次 - 并且同一电子邮件仅发送给列表中的第一个收件人 x 收件人数量。 (多次)。

触发行为的多个收件人列表示例

以.ps1文件形式使用的代码

# Import user list and information from .CSV file
$Users = Import-Csv UserList.csv

# Email details
Foreach ($User in $Users) {
$sender = "[email protected]"
$recipient = $User.Email
$subject = "Test Email"
$type = "HTML"
$save = "true"

$body = @"
Receiving email went through.
"@

# Import MgGraph module
Import-Module Microsoft.Graph.Users.Actions

# Connect to MgGraph with required permissions
Connect-MgGraph -Scopes 'Mail.Send', 'Mail.Send.Shared'

$params = @{
    Message         = @{
        Subject       = $subject
        Body          = @{
            ContentType = $type
            Content     = $body
        }
        ToRecipients  = @(
            @{
                EmailAddress = @{
                    Address = $recipient
                }
            }
        )
    }
    SaveToSentItems = $save
}
}
# Send message
Send-MgUserMail -UserId $sender -BodyParameter $params

任何有关如何解决此问题的建议将不胜感激。同样,只有在向 .csv 文件添加多个电子邮件收件人时才会遇到该错误,但在仅添加一个收件人时它可以正常工作。

提前谢谢您。

© www.soinside.com 2019 - 2024. All rights reserved.