1

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" } 
3
  • 3
    Maybe unrelated, but the From values of you hashtables are missing a closing double quote. Commented May 5, 2017 at 13:42
  • 1
    The code tries to send two emails, and you say "the email sends correctly" - simple answer is that one sends correctly and one fails...? Commented May 5, 2017 at 13:44
  • Might be worth adding -Verbose to Send-MailMessage to help with your fault finding. Commented May 5, 2017 at 13:49

1 Answer 1

2

My guess would be that $Attachment = Get-ChildItem -Path $dir -Filter "*$($Region.Name)*" -Recurse is not returning any files for one or more of the regions, so $Subject ends up being $null.

You could either check for that state and perhaps throw a warning instead of attempting to send the mail, or another way to work around the error (and get an email sent but with a blank subject) would be to add some other (guaranteed) text to $subject. E.g:

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

Although then I suspect it would complain about -Attachments being null.

To add a check/throw warning, you could do the following:

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." } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.