首页 > 解决方案 > 为什么我无法连接到我的服务器(连接尝试失败)

问题描述

在此处输入图像描述我使用了 mailkit 并且 100% 与 Outlook smtp 一起工作,但不是我自己的域。

 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 子句:

捕捉(异常前){

            //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();

标签: c#smtpmailkit

解决方案


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


推荐阅读