C# 3.0:查找域内的 SMTP 服务器
我使用的是 C# 3.0 和 System.DirectoryServices 命名空间(不是 .NET 3.5 的较新的 System.DirectoryServices.AccountManagement 命名空间)。 如何找到本地域上的所有 SMTP 服务器? 这可能吗? 还有其他方法可以实现此目的吗?
I'm using C# 3.0 and the System.DirectoryServices
namespace (not the newer System.DirectoryServices.AccountManagement
namespace of .NET 3.5). How can I find all of the SMTP Servers on the local domain? Is this even possible? Is there another way to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
另一种方法是执行 DNS MX(邮件交换记录)查询来查找给定域的 SMTP 服务器:
代码项目示例
Egghead示例(抱歉,找不到原帖)
A different approach would be to do DNS MX (Mail exchange record) queries to find SMTP servers for a given domain:
Code Project Sample
Egghead sample (sorry, could not find the original post)
我怀疑域服务器明确发布了它们是 SMTP 服务器的事实(我可能是错的),尽管解决方案应该非常简单。
220
响应,这表明服务器已准备就绪。 (有关协议,请参阅 RFC 文档。)如果您在一定时间内收到此命令连接后的时间(例如3秒),则可以断定当前计算机是SMTP服务器。希望有帮助。
I doubt that domain servers explicitly publish the fact that they are SMTP servers (I may be wrong), though the solution should be quite simple nonetheless.
220
response, which indicates that the server is ready. (See the RFC document for the protocol.) If you receive this command within a certain time after connect (say, 3 seconds), then you can conclude that the current computer is a SMTP server.Hope that helps.
我认为您不能使用 DirectoryServices 做到这一点。
一种选择是尝试通过 SMTP 端口 (25) 连接到域中的每台服务器,并查看它们是否响应标准 SMTP 命令。 如果您有域中计算机的列表,则可以使用 TcpClient 类轻松完成此操作。
当然,这不会找到不使用标准端口的服务器(但如果服务器不使用标准端口,它可能对首先被发现不感兴趣:-)
I do not think you can do that with DirectoryServices.
One option would be to attempt a connection to each server on the domain on the SMTP port (25), and see if they respond to standard SMTP commands. This could easily be done using the TcpClient class, if you have a list of the machines in the domain.
Of course, that would not find servers not using the standard port (but if the server is not using the standard port, it might not be interested in being found in the first place :-)
根据Noldorin的建议,这里有一些代码,注意我只是在25上连接,我没有等待来自服务器的220。 这在我们的域上有效。 这是一个残酷的正则表达式,用于根据 LDAP 路径获取服务器名称。
另外,我认为 FindAll 受到通常的 AD 约束,即 1000 个结果,因此如果您的域中有超过 1000 个服务器,您可能需要重新工作
Based on Noldorin's suggestion, here is some code, note I just connect on 25, I'm not waiting for the 220 from the server. This worked on our domain. This is brutal regex to get the server name based on the LDAP path.
Also, I think FindAll suffers from the usual AD constraint, which is a 1000 results , so if you have more than a 1000 servers in your domain, you might have to rework
如果您想找到某个域的邮件服务器以便将邮件发送到该域,那么使用 DNS MX 是最佳选择,正如 mjmarh 已经建议的那样。
如果您想使用 AD 识别域中的所有任意 SMTP 服务,那么您可以利用大多数 SMTP 服务器将在 AD 中注册自身的事实,例如 Exchange 就是如此,并且您可以询问 AD 服务以找出它们的位置。 例如,本白皮书介绍了 Outlook 客户端如何使用 Active Directory 发现其邮箱服务器:http ://technet.microsoft.com/en-us/library/bb332063.aspx
在某些域上,对所有计算机进行端口扫描将点亮它们拥有的任何入侵检测机制,就像圣诞树一样,您最终可能会关闭应用程序网络地址。
If you want to find the mail server of a domain in order to send mail to that domain then using the DNS MX is the way to go, as mjmarh already suggested.
If you want to identify all arbitrary SMTP services in your domain using AD then you could leverage the fact that most SMTP server will register themselves in AD, for example Exchange does, and you can interrogate the AD services to find out their location. For example this white paper explains how Outlook clients discover their mailbox server using Active Directories: http://technet.microsoft.com/en-us/library/bb332063.aspx
On certain domain doing a port scan on all machines will lit up any intrusion detection mechanism they have like a Christmas Tree and you may end up with your application network address shut down.