I am attempting to write a powershell script to send email with a few attachments to 30 people. The emails are personalized, so they must be sent individually. The script works just fine without attachments. However, when using attachments, every other instance of Send-MailMessage fails with:
Send-MailMessage : Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.
It does not seem to matter how long I wait/pause between sending messages. Even if I wait several minutes, the first instance will always succeed, the next will fail, the next will succeed, etc.. I've even noticed between running scripts, that my ctrl+c on even or odd numbers determines the success of the first instance of Send-MailMessage. If the last message fails, the first succeeds, and vice versa.
My code is very simple, we just have a single array with all the users, and
$array | foreach {
Write-Host "Sending Mail..."
Send-MailMessage -From 'myemail@domain' -To $_.EmailAddress
-SmtpServer 'fqdn'
-Attachments "1.pdf", "2.pdf"
-Subject 'Subject'
-Body ($html)
-BodyAsHtml
}
So the output will be something along the lines of:
Sending Mail...
Sending Mail...
Send-MailMessage : Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.
Sending Mail...
Sending Mail...
Send-MailMessage : Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.
And in the instances where there are no errors, the mail arrives perfectly intact.
I can "fix" this by just doing:
try { Send-MailMessage ... -EA Stop }
catch { Send-MailMessage }
And as a result, 30/30 people will receive the mail (rather than 15/30), since every single try (besides #1) fails but the code in the catch block is always able to succeed.
Of course, this isn't a real solution and I would hate to leave it like that. Does anyone have any insight into what is going on here, and how to fix it?