为什么我无法连接到我的服务器(连接尝试失败)

时间:2018-05-16 22:17:03

标签: c# smtp mailkit

enter image description here我使用mailkit并且使用outlook smtp 100%工作,但不是我自己的域。

 using (var client = new SmtpClient())
            {

                client.AuthenticationMechanisms.Remove("XOAUTH2");
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                client.Connect(SmtpServer, SmtpPortNumber, /*SecureSocketOptions.Auto*/ /*SSL*/true);
                // Note: only needed if the SMTP server requires authentication 
                // Error 5.5.1 Authentication  
                client.Authenticate(UsernameD, PasswordD);
                client.Send(mimeMessage);

                client.Disconnect(true);
            }

catch子句:

catch(Exception ex)             {

            //when any error occurrs in this method the error message is written in the log file (log.txt) which is located in the root.
            using (StreamWriter writer = new StreamWriter(LogError, true))
            {
                writer.WriteLine("Message :" + ex.Message + "<br/>" + Environment.NewLine + "StackTrace :" + ex.StackTrace +
                   "" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
                writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
            }
            Application.ExitThread();
            Application.Exit();

1 个答案:

答案 0 :(得分:0)

确保桌面上没有防火墙阻止您连接到服务器。还要确保服务器没有防火墙阻止运行smtp服务的任何端口上的传入连接(25?465?587?)