C# 3.0:查找域内的 SMTP 服务器

发布于 2024-07-19 04:31:16 字数 165 浏览 7 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独﹏钓一江月 2024-07-26 04:31:16

另一种方法是执行 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)

左耳近心 2024-07-26 04:31:16

我怀疑域服务器明确发布了它们是 SMTP 服务器的事实(我可能是错的),尽管解决方案应该非常简单。

  • 查找活动域内的每台服务器。
  • 尝试通过端口 25 (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.

  • Find each server within the active domain.
  • Attempt to connect to the server on port 25 (SMTP).
  • Wait for a 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.

一场信仰旅途 2024-07-26 04:31:16

我认为您不能使用 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 :-)

浴红衣 2024-07-26 04:31:16

根据Noldorin的建议,这里有一些代码,注意我只是在25上连接,我没有等待来自服务器的220。 这在我们的域上有效。 这是一个残酷的正则表达式,用于根据 LDAP 路径获取服务器名称。

static void Main()
        {

             DirectorySearcher ds = new DirectorySearcher("");
             ds.Filter = "objectCategory=computer";
             SearchResultCollection results = ds.FindAll();
             foreach (SearchResult result in results)
             {
                 string pattern = @"(?<=LDAP://CN=)(?<serverName>\w*)(?=,*)";
                 Match m = Regex.Match(result.Path, pattern);
                 string serverName = m.Groups["serverName"].Value;

                 System.Net.Sockets.TcpClient tcp = new System.Net.Sockets.TcpClient();
                 try
                 {
                     tcp.Connect(serverName, 25);

                     if (tcp.Connected)
                     {
                         Console.WriteLine(String.Format("Connected to {0} on Port 25", serverName));
                     }
                 }

                 catch (Exception ex)
                 {
                     Console.WriteLine("Exception: " + ex.Message);
                 }

                 finally
                 {
                     tcp.Close();
                 }

             }

             Console.WriteLine("Done.");
             Console.ReadLine();
}

另外,我认为 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.

static void Main()
        {

             DirectorySearcher ds = new DirectorySearcher("");
             ds.Filter = "objectCategory=computer";
             SearchResultCollection results = ds.FindAll();
             foreach (SearchResult result in results)
             {
                 string pattern = @"(?<=LDAP://CN=)(?<serverName>\w*)(?=,*)";
                 Match m = Regex.Match(result.Path, pattern);
                 string serverName = m.Groups["serverName"].Value;

                 System.Net.Sockets.TcpClient tcp = new System.Net.Sockets.TcpClient();
                 try
                 {
                     tcp.Connect(serverName, 25);

                     if (tcp.Connected)
                     {
                         Console.WriteLine(String.Format("Connected to {0} on Port 25", serverName));
                     }
                 }

                 catch (Exception ex)
                 {
                     Console.WriteLine("Exception: " + ex.Message);
                 }

                 finally
                 {
                     tcp.Close();
                 }

             }

             Console.WriteLine("Done.");
             Console.ReadLine();
}

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

因为看清所以看轻 2024-07-26 04:31:16

如果您想找到某个域的邮件服务器以便将邮件发送到该域,那么使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文