使用附件时,Send-MailMessage 关闭每个第二个连接 [英] Send-MailMessage closes every 2nd connection when using attachments

查看:34
本文介绍了使用附件时,Send-MailMessage 关闭每个第二个连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 powershell 脚本来向 30 个人发送带有一些附件的电子邮件.电子邮件是个性化的,因此必须单独发送.该脚本无需附件即可正常工作.但是,在使用附件时,Send-MailMessage 的所有其他实例都会失败并显示:

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 :无法将数据写入传输连接:现有连接被远程主机强行关闭.

Send-MailMessage : Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.

我在发送消息之间等待/暂停多长时间似乎并不重要.即使我等待几分钟,第一个实例总是会成功,下一个会失败,下一个会成功,等等.我什至注意到在运行脚本之间,我在偶数或奇数上的 ctrl+c 决定了 Send-MailMessage 的第一个实例的成功.如果最后一条消息失败,则第一条消息成功,反之亦然.

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:

发送邮件...

发送邮件...

Send-MailMessage :无法将数据写入传输连接:现有连接被远程主机强行关闭.

Send-MailMessage : Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.

发送邮件...

发送邮件...

Send-MailMessage :无法将数据写入传输连接:现有连接被远程主机强行关闭.

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 }

因此,30/30 的人将收到邮件(而不是 15/30),因为每次尝试(除了 #1)都会失败,但 catch 块中的代码总是能够成功.

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?

推荐答案

我相信我已经找到了最好的解决方案.非常感谢 Matt 在评论中帮助我解决这个问题.

I believe I have found the best available solution to this. Big thanks to Matt for helping me with this in the comments.

这个问题似乎源于 Send-Mailmessage 在完成发送邮件后没有正确处理连接对象.使用现有连接运行 Send-Mailmessage 会强制释放它,因此第三次运行它会成功.

It seems like the issue spawns from Send-Mailmessage not properly disposing the connection object once it finishes sending mail. Running Send-Mailmessage with an existing connection forces it to be disposed, and thus running it for a third time results in success.

解决方法是将 Send-Mailmessage 的每个实例作为单独的作业运行.引用马特:

The workaround is running each instance of Send-Mailmessage as a separate job. To quote Matt:

PowerShell 作业有自己的内存和资源.当工作是完成后应该删除内存.

PowerShell jobs have their own memory and resources. When the job is done that memory is supposed to be removed.

因此,每次我们将 Send-Mailmessage 作为作业运行时,都会正确创建和处理连接.我也将它传递给 Wait-Job |Receive-Job 可以自然地限制速率、查看输出并防止理论上可能存在的任何内存问题.

As a result, each time we run Send-Mailmessage as a job, the connection is properly created and disposed. I am also piping this to Wait-Job | Receive-Job to naturally rate-limit, view output, and prevent any memory issues that could maybe be theoretically possible.

Start-Job -ScriptBlock {
  Send-MailMessage -From 'mymail' -To 'theirmail' -SmtpServer 'fqdn' -Attachments "$($args[0])1.pdf", "$($args[0])2.pdf" -Subject 'subject' -Body ("test")
} -ArgumentList $PSScriptRoot | Wait-Job | Receive-Job

使用此方法不会产生错误,并且应该减少 SMTP 服务器的负载.

Using this method produces no errors, and should reduce load on the SMTP server.

这篇关于使用附件时,Send-MailMessage 关闭每个第二个连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆