.NET 发送电子邮件的最佳方法(System.Net.Mail 有问题) [英] .NET Best Method to Send Email (System.Net.Mail has issues)

查看:38
本文介绍了.NET 发送电子邮件的最佳方法(System.Net.Mail 有问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很简单.我需要从一些 ASP.NET 应用程序发送电子邮件.我需要始终如一地做到这一点,而不会出现奇怪的错误,也不会出现 CPU 利用率过高的情况.我不是在谈论群发电子邮件,只是偶尔发送电子邮件.

This seems to be pretty straight forward. I need to send email from some ASP.NET applications. I need to do this consistently without strange errors and without CPU utilization going through the roof. I'm not talking about mass emailing, just occasional emails.

System.Net.Mail 似乎被严重破坏了.SmtpClient 不发出 Quit 命令(可能是因为 Microsoft(R) 对以下规范不感兴趣),因此连接保持打开状态.因此,如果有人试图在该连接最终关闭之前发送电子邮件,您可能会从 SMTP 服务器收到关于打开的连接过多的错误.这是 Microsoft(R) 对修复完全不感兴趣的错误.见这里:

System.Net.Mail appears to be horribly broken. The SmtpClient does not issue the Quit command (it may be because Microsoft(R) is not interested in following specifications), therefore a connection is left open. Therefore, if someone tries to email before that connection finally closes, you can get errors from the SMTP Server about too many connections open. This is a bug that Microsoft(R) is completely uninterested in fixing. See here:

http://connect.microsoft.com/VisualStudio/feedback/查看Feedback.aspx?FeedbackID=146711

另外,如果您环顾四周,建议使用此代码来解决此问题:

Also, if you look around some suggest to use this code to solve this problem:

smtpClient.ServicePoint.MaxIdleTime = 1;
smtpClient.ServicePoint.ConnectionLimit = 1;

好的,是的,这确实解决"了连接保持打开状态的问题.但是,这始终如一,如果您愿意,可以在服务器上尝试,这会导致运行进程(在本例中为 w3wp.exe)的 CPU 跳跃并保持在 100%,直到您的应用程序池被回收.无论出于何种原因,运行 mscorwks.dll!CreateApplicationContext 的线程都是罪魁祸首.

Ok, yes that does "solve" the problem of connections being left open. However, this consistently, try it on a server if you like, causes the CPU on which the process (in this case w3wp.exe) is running to jump and remain at 100% until your application pool is recycled. For whatever reason, the thread that runs mscorwks.dll!CreateApplicationContext is the culprit.

这有一个非常好的副作用,如果您在一个对持续 100% 的 CPU 使用不满意的 Web 主机上运行,​​您将禁用您的应用程序池.因此,这并不像某些人建议的那样微不足道.

This has the very nice side effect that if you are running on a web host that frowns on sustained 100% CPU usage, you will get your application pool disabled. So this is not as trivial as some suggest.

所以我的问题是怎么办?我需要做的就是这么简单;然而,让那些打开的连接太多"错误是不可接受的,100% 的 CPU 使用率也是不可接受的.我不想购买第三方组件,不是因为我便宜,而是我购买了足够多的组件和 MSDN 订阅,以至于不得不为简单的 SMTP 功能支付 100-300 美元似乎很疯狂.

So my question is what to do? What I need to do is so simple; however getting those "too many connections open" errors is not acceptable and nor is the 100% CPU usage. I don't want to purchase a third party component, not because I'm cheap, but I buy enough components and MSDN subscription that it seems crazy to have to shell out $100-$300 for simple SMTP functionality.

我读到将 MaxIdleTime 设置得更高会有所帮助,但我对此表示怀疑.我不想因为 Microsoft 不想遵循 SMTP 规范而冒险禁用我的应用程序池.

I read that setting the MaxIdleTime higher can help but I'm skeptical of that. I don't want to risk my app pool being disabled just because Microsoft doesn't want to follow the SMTP specification.

我查看了 quiksoft.com 的组件,但它不支持 SMTP 身份验证,而且价格为 500 美元.一定有办法解决这个问题.

I looked at quiksoft.com components, however it does not support SMTP authentication and it costs $500. There's got to be a solution to this problem.

推荐答案

我遇到了与所述设置相同的 CPU 利用率问题.我最终向 Microsoft 开了一张票,以确定问题的原因.CPU 利用率问题在于 ServicePoint 类.在 ServicePoint 类内部,有一个每 (MaxIdleTime/2) 毫秒运行一次的计时器.看到问题了吗?通过将 MaxIdleTime 值更改为 2,CPU 利用率将下降到正常水平.

I faced the same CPU utilization problem with the settings described. I ended up opening a ticket with Microsoft to determine the cause of the problem. The CPU utilization problem lies in the ServicePoint class. Internally in the ServicePoint class, there is a timer that runs every (MaxIdleTime/2) milliseconds. See the problem? By changing the MaxIdleTime value to 2, the CPU utilization will drop down to normal levels.

这篇关于.NET 发送电子邮件的最佳方法(System.Net.Mail 有问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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