system.net.mail.smptclient 是否与服务器断开连接?
我们的 Web 系统中有一些页面使用 .net system.net.mail 控件来发送电子邮件。 事情一直运行良好,只是现在开始看起来 smptclient 类实际上可能没有与服务器断开连接,因此 SMTP 服务器使该连接保持打开状态,并且我们最终超出了允许的连接数尽管一次只发送一封电子邮件,但在 SMTP 服务器上一次打开。
(根据记录,这是一个用 VB 编写的 .net 2.0 asp.net 应用程序,我们相当有信心这不是某种安全/病毒/垃圾邮件传递情况。)
Google 和 MSDN 没有发现任何内容结论是,但博客文章中的异端邪说足以证实我们可能不是幻觉。
还有其他人遇到过这个问题吗? (并设法修复它?)
当然,如果它确实工作正常,而我们产生了幻觉,那么也很高兴知道这一点。 ;)
We've got a few pages in our web systems that use the .net system.net.mail control to send emails. The thing has been working great, except it's now starting to look like the smptclient class may not actually be disconnecting from the server, such that the SMTP server leaves that connection open, and we ended up maxing out the number of connections we were allowed to have open at a time on the SMTP server, despite only sending one email at a time.
(For the record, this is a .net 2.0 asp.net application written in VB, and we're fairly confident this isn't some kind of security / virus / spam passthrough situation.)
Google and MSDN didn't turn up anything conclusive, but just enough heresy in blog entries to confirm that we might not be hallucinating.
Any one else out there ever have this problem? (And manage to fix it?)
Of course, if it does work fine, and we are hallucinating, that would be nice to know too. ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 SmtpClient 无法立即发送电子邮件的问题是因为在某些情况下,它在应该发送 SMTP 命令“QUIT”时没有发送(即,它没有正确断开连接)。 我过去曾使用以下代码成功强制断开连接:
这会强制 smtpclient 对象尽早断开连接。 另外,请确保您的 smtpclient 实例很快就会超出范围(即不要在某处存储对其的静态引用)。
The problem of emails not being sent right away with SmtpClient is because under some conditions, it does not send the SMTP command 'QUIT' when it should (i.e. it doesn't disconnect properly). I have used the following code to force a disconnect successfully in the past:
This forces the smtpclient object to disconnect at the earliest possible time. Also, make sure your smtpclient instance falls out of scope fairly quickly (i.e. do not store a static reference to it somewhere).
MailMessage 发送后,您是否正确处理它? 我从未见过任何迹象表明 SmtpClient 没有关闭它的连接
Are you properly disposing of your MailMessage once it has been sent? I've never seen any indication that SmtpClient wasn't closing it's connection though
您是否检查了 StmpClient 类的 StmpDeliveryMthod 属性?
Did you check the StmpDeliveryMthod property of the StmpClient class?
对于此类“我是否已连接”问题,一个方便的工具是 SysInternals(现为 Microsoft)的 TcpView ( \\live.sysinternals.com\Tools\Tcpview.exe )
A handy tool for this type of 'am I connected' question is TcpView ( \\live.sysinternals.com\Tools\Tcpview.exe ) from SysInternals (now Microsoft)
确保在邮件服务器上设置这些(假设是 Windows) -
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]
“TcpTimedWaitDelay”=dword:0000001e
“MaxFreeTcbs”=dword:000007d0
“最大用户端口”=dword:0000fffe
Make sure these are set on the mail server (assuming windows) -
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]
"TcpTimedWaitDelay"=dword:0000001e
"MaxFreeTcbs"=dword:000007d0
"MaxUserPort"=dword:0000fffe