.Net TcpClient和SmtpClient无法连接到我的Smtp服务器

时间:2014-06-28 23:50:08

标签: c# .net smtp tcpclient smtpclient

我有一个方法应该连接到Smtp服务器以查看它是否在线。此方法在测试许多邮件服务器时有效,但不是全部。代码如下,但失败发生在......

client.Connect(strMailServer,intPort);

...在与服务器交谈的逻辑开始之前。它根本不会连接。我已经确定我正在连接正确的IP和端口(25),并且我使用了像mxtoolbox这样的第三方网站来成功测试相同的服务器IP。这台服务器正在接收来自万维网的常规流量...只有.Net似乎无法连接。我已经查看了防火墙规则,并观看了WireShark以查看服务器上发生了什么,但我从未看到来自我的测试运行的任何传入数据包。防火墙设置为允许所有接口上的端口25的所有连接。

我还使用SmtpClient运行如下所示的类似测试,它也失败了。

                var client = new System.Net.Mail.SmtpClient(strMailServer, intPort);
            client.Send("test@mydomain.com", "test@mydomain.com", "test message", "This is meant to test an SMTP server to see if it is online, it expects the message to be rejected.");

此处的错误堆栈导致与我的TcpClient尝试相同的欠载错误。 SocketException:{“无法建立连接,因为目标计算机主动拒绝它xxx.xxx.xxx.xxx:25”}

世界上的每个人怎么能够连接到这台服务器...除了我的笔记本电脑......我不认为这是防火墙问题。

帮助!

public static bool TestMailServer(string strMailServer, int intPort, out string strResponse)
    {
        try
        {
            try
            {
                //First I'll try a basic SMTP HELO
                using (var client = new TcpClient())
                {
                    client.Connect(strMailServer, intPort);
                    // As GMail requires SSL we should use SslStream
                    // If your SMTP server doesn't support SSL you can
                    // work directly with the underlying stream
                    using (var stream = client.GetStream())
                    {
                        using (var writer = new StreamWriter(stream))
                        using (var reader = new StreamReader(stream))
                        {
                            writer.WriteLine("EHLO " + strMailServer);
                            writer.Flush();
                            strResponse = reader.ReadLine();
                            if (strResponse == null)
                                throw new Exception("No Valid Connection");

                            stream.Close();
                            client.Close();
                            if (F.StartsWith(strResponse, "220"))
                                return true;
                            else
                                return false;
                        }

                    }

                }
            }
            catch (Exception ex)
            {
                //If the above failed, I'll try with SSL
                using (var client = new TcpClient())
                {
                    //var server = "smtp.gmail.com";
                    //var port = 465;
                    //client.SendTimeout = 10000;
                    //client.ReceiveTimeout = 10000;

                    client.Connect(strMailServer, intPort);
                    // As GMail requires SSL we should use SslStream
                    // If your SMTP server doesn't support SSL you can
                    // work directly with the underlying stream
                    using (var stream = client.GetStream())
                    using (var sslStream = new SslStream(stream))
                    {
                        sslStream.AuthenticateAsClient(strMailServer);
                        using (var writer = new StreamWriter(sslStream))
                        using (var reader = new StreamReader(sslStream))
                        {
                            writer.WriteLine("EHLO " + strMailServer);
                            writer.Flush();

                            strResponse = reader.ReadLine();
                            if (strResponse == null)
                                throw new Exception("No Valid Connection");

                            stream.Close();
                            client.Close();
                            if (F.StartsWith(strResponse, "220"))
                                return true;
                            else
                                return false;
                            // GMail responds with: 220 mx.google.com ESMTP
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            strResponse = ex.Message;
            return false;
        }
    }

3 个答案:

答案 0 :(得分:2)

"目标机器主动拒绝它"意味着没有任何东西在端口25上侦听 - 你发送了一个SYN数据包并且返回了RST而不是SYN / ACK。

如果你甚至没有在wirehark捕获中看到SYN,也许你的笔记本电脑正在正确地解析名称。您是通过名称还是IP连接到服务器?你试过一个简单的

吗?
telnet mailhost 25

看看是否连接?

答案 1 :(得分:0)

通常,为防止SMTP服务器发送匿名电子邮件,只有在入站IP地址成功进行反向DNS查找后,他们才会接受连接。我相信您家庭地址的反向DNS查询失败,因此它已断开连接。

答案 2 :(得分:0)

答案很简单我打了个I我意识到......我的ISP阻止了端口25上的所有连接,入站和出站。一旦我搬到另一个地方,一切都运转了。咄!!

http://www.cox.com/residential/support/internet/article.cox?articleId=cacf82f0-6407-11df-ccef-000000000000

对于谁可能想要它,这是我的SMTPTest类,它将探测服务器的SMTP功能。我用它来监控我的服务器,报告中断,并验证用户输入的服务器信息。

public struct SMTPTestResult
{
    public string Server;
    public bool Found;
    public bool OriginalPortSuccess;
    public int FinalPort;
    public bool UsedSSL;
    public string Response;
}

public class SMTPTest
{
    public static SMTPTestResult TestMailServer(string MailServer)
    {
        return TestMailServer(MailServer, 25, true);
    }

    public static SMTPTestResult TestMailServer(string MailServer, int Port, bool TryOtherPorts)
    {
        SMTPTestResult result = new SMTPTestResult();
        result.Server = MailServer;

        if (AttemptMailServer(MailServer, Port, false, out result.Response))
        {
            //First try the requested port, without SSL.
            result.Found = true;
            result.UsedSSL = false;
            result.OriginalPortSuccess = true;
            result.FinalPort = Port;
            return result;
        }
        else if (AttemptMailServer(MailServer, Port, true, out result.Response))
        {
            //Try the requested port, with SSL.
            result.Found = true;
            result.UsedSSL = true;
            result.OriginalPortSuccess = true;
            result.FinalPort = Port;
            return result;
        }
        else if (TryOtherPorts && Port != 465 && AttemptMailServer(MailServer, 465, true, out result.Response))
        {
            //Try port 465 with SSL
            result.Found = true;
            result.UsedSSL = true;
            result.OriginalPortSuccess = false;
            result.FinalPort = 465;
            return result;
        }
        else if (TryOtherPorts && Port != 25 && AttemptMailServer(MailServer, 25, false, out result.Response))
        {
            //Try port 25, without SSL.
            result.Found = true;
            result.UsedSSL = false;
            result.OriginalPortSuccess = false;
            result.FinalPort = 25;
            return result;
        }
        else if (TryOtherPorts && Port != 25 && AttemptMailServer(MailServer, 25, true, out result.Response))
        {
            //Try port 25, with SSL.
            result.Found = true;
            result.UsedSSL = true;
            result.OriginalPortSuccess = false;
            result.FinalPort = 25;
            return result;
        }
        else if (TryOtherPorts && Port != 587 && AttemptMailServer(MailServer, 587, false, out result.Response))
        {
            //Try port 587, without SSL.
            result.Found = true;
            result.UsedSSL = false;
            result.OriginalPortSuccess = false;
            result.FinalPort = 587;
            return result;
        }
        else if (TryOtherPorts && Port != 587 && AttemptMailServer(MailServer, 587, true, out result.Response))
        {
            //Try port 587, with SSL.
            result.Found = true;
            result.UsedSSL = true;
            result.OriginalPortSuccess = false;
            result.FinalPort = 587;
            return result;
        }
        else
        {
            result.Found = false;
            result.OriginalPortSuccess = false;
            result.FinalPort = Port;
            return result;
        }
    }

    private static bool AttemptMailServer(string strMailServer, int intPort, bool blnSSL, out string strResponse)
    {
        try
        {
            if(!blnSSL)
            {
                //I'll try a basic SMTP HELO
                using (var client = new TcpClient())
                {
                    client.Connect(strMailServer, intPort);
                    using (var stream = client.GetStream())
                    {
                        using (var writer = new StreamWriter(stream))
                        using (var reader = new StreamReader(stream))
                        {
                            writer.WriteLine("EHLO " + strMailServer);
                            writer.Flush();
                            strResponse = reader.ReadLine();
                            if (strResponse == null)
                                throw new Exception("No Valid Connection");

                            stream.Close();
                            client.Close();
                            if (strResponse.StartsWith("220"))
                                return true;
                            else
                                return false;
                        }

                    }
                }
            }
            else
            {
                //I'll try with SSL
                using (var client = new TcpClient())
                {
                    client.Connect(strMailServer, intPort);
                    // As GMail requires SSL we should use SslStream
                    // If your SMTP server doesn't support SSL you can
                    // work directly with the underlying stream
                    using (var stream = client.GetStream())
                    using (var sslStream = new SslStream(stream))
                    {
                        sslStream.AuthenticateAsClient(strMailServer);
                        using (var writer = new StreamWriter(sslStream))
                        using (var reader = new StreamReader(sslStream))
                        {
                            writer.WriteLine("EHLO " + strMailServer);
                            writer.Flush();

                            strResponse = reader.ReadLine();
                            if (strResponse == null)
                                throw new Exception("No Valid Connection");

                            stream.Close();
                            client.Close();
                            if (strResponse.StartsWith("220"))
                                return true;
                            else
                                return false;
                            // GMail responds with: 220 mx.google.com ESMTP
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            strResponse = ex.Message;
            return false;
        }
    }

}