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

查看:165
本文介绍了使用附件时,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
}

所以输出将类似于:


发送邮件...

Sending Mail...

发送邮件...

发送邮件消息:无法将数据写入传输连接:
远程主机强行关闭了现有连接。

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 }

因此,由于每次尝试(除了#1之外),都会有30/30人收到邮件(而不是15/30)。 )失败,但是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 的每个实例作为单独的作业运行。引用@Matt:

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.

因此,每次我们运行发送邮件消息作为作业,可以正确创建和处理连接。我也将此内容发送到 Wait-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天全站免登陆