SmtpClient.Send() 在服务器上占用很长时间和超时



>我正在使用此代码发送和发送电子邮件。

StringBuilder Emailbody = new StringBuilder();
                    Emailbody.Append("Hello World!");
                    MailMessage mail = new MailMessage();
                    mail.To.Add("ziapitafi@mremind.com");
                    mail.From = new MailAddress("MaxRemindHealthSystem@mremind.com");
                    mail.Subject = " MaxRemind User PIN Code";
                    mail.Body = Emailbody.ToString();
                    mail.IsBodyHtml = true;
                    SmtpClient smtpClient = new SmtpClient();
                    smtpClient.Host = "smtpout.secureserver.net";
                    smtpClient.Port = 25;
                    smtpClient.EnableSsl = false;
                    smtpClient.Credentials = new System.Net.NetworkCredential("MaxRemindHealthSystem@mremind.com", "pass");
                    smtpClient.Send(mail);

此代码在本地主机上正常工作,我收到一封电子邮件。但是当我在服务器上部署此代码时smtpClient.Send(mail);花费太多时间,然后抛出超时异常

因此它在本地工作,我怀疑服务器无法与邮件/smtp服务器通信,例如防火墙规则。我也没有在您的代码中看到表明存在一些问题的问题。

为了进行诊断,您可以启动 telnet 客户端(例如 putty(,并尝试使用 telnet 模式和端口 25 连接到 smtp 服务器主机名。

您也可以尝试使用 c# TcpClient 本身对其进行诊断:

        using (TcpClient tcpClient = new TcpClient())
        {
            Console.WriteLine("Connecting...");
            try
            {
                tcpClient.Connect(smptServer, 25);
            }
            catch (SocketException e)
            {
                Console.WriteLine("Connection error: {0}", e.Message);
                return;
            }

            if (!tcpClient.Connected)
            {
                Console.WriteLine("Unknown connection error...");
                return;
            }
            // get stream
            NetworkStream networkStream = null;
            Console.WriteLine("Get stream...");
            try
            {
                networkStream = tcpClient.GetStream();
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine("Stream error: {0}", e.Message);
                return;
            }
            finally
            {
                networkStream.Close();
                networkStream.Dispose();
                tcpClient.Close();
            }
            Console.WriteLine("Connection successfull...")
        }

如果我的假设是正确的,那么甚至不可能建立联系。

最新更新