This error appears when running the script below:
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.
The email still sends successfully and the subject appears correctly.
$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 = "[email protected] To = "[email protected]" Cc = "[email protected]" } $ireland = @{ Name = 'Ireland' From = "[email protected] To = "[email protected]" Cc = "[email protected]" } $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" }
Fromvalues of you hashtables are missing a closing double quote.-VerbosetoSend-MailMessageto help with your fault finding.