gpt4 book ai didi

windows - 发送邮件消息 : Cannot validate argument on parameter 'Subject'

转载 作者:可可西里 更新时间:2023-11-01 11:46:20 26 4
gpt4 key购买 nike

运行以下脚本时出现此错误:

Send-MailMessage : Cannot validate argument on parameter 'Subject'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

邮件仍然发送成功,主题正确显示。

$dir = "C:\Users\user\Desktop\Lists\TodaysLists"
$SMTPServer = "192.168.1.111"
$Time = (Get-Date).ToString('MM/dd/yyyy hh:mm tt')

$japan = @{
Name = 'Japan'
From = "me@me.com
To = "you@me.com"
Cc = "him@me.com"
}

$ireland = @{
Name = 'Ireland'
From = "me@me.com
To = "you@me.com"
Cc = "him@me.com"
}

$Regions = @()
$Regions += New-Object PSObject -Property $japan
$Regions += New-Object PSObject -Property $ireland

foreach ($Region in $Regions) {
$Attachment = Get-ChildItem -Path $dir -Filter "*$($Region.Name)*" -Recurse
$AttachmentName = $Attachment.BaseName
$Subject = "$AttachmentName"
$Body = "Please find attached the Report for $($Region.Name).

Produced @ $Time

Regards,
John Doe
"
Send-MailMessage -From $Region.From -To $Region.To -CC $Region.Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Attachments $Attachment.FullName
$Attachment | Move-Item -Destination "C:\Users\user\Desktop\Lists\oldLists"
}

最佳答案

我的猜测是 $Attachment = Get-ChildItem -Path $dir -Filter "*$($Region.Name)*"-Recurse 不会返回一个或多个的任何文件地区,所以 $Subject 最终成为 $null

您可以检查该状态并可能发出警告而不是尝试发送邮件,或者另一种解决错误的方法(并发送一封电子邮件但主题为空)是添加一些其他的(保证)文本到 $subject。例如:

$Subject = "$($Region.Name): $AttachmentName"

尽管我怀疑它会提示 -Attachments 为 null。

要添加检查/抛出警告,您可以执行以下操作:

foreach ($Region in $Regions) {
$Attachment = Get-ChildItem -Path $dir -Filter "*$($Region.Name)*" -Recurse

If ($Attachment) {

$AttachmentName = $Attachment.BaseName
$Subject = "$AttachmentName"
$Body = "Please find attached the Report for $($Region.Name).

Produced @ $Time

Regards,
John Doe
"
Send-MailMessage -From $Region.From -To $Region.To -CC $Region.Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Attachments $Attachment.FullName
$Attachment | Move-Item -Destination "C:\Users\user\Desktop\Lists\oldLists"
} Else {
Write-Warning "One or more files named $($Region.Name) were not found in $dir. Mail not sent."
}
}

关于windows - 发送邮件消息 : Cannot validate argument on parameter 'Subject' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43806339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com