Can I test SmtpClient before calling client.Send()?

前端 未结 6 783
夕颜
夕颜 2020-11-28 10:58

This is related to a question I asked the other day on how to send email.

My new, related question is this... what if the user of my application is behind a firew

6条回答
  •  被撕碎了的回忆
    2020-11-28 11:38

        private bool isValidSMTP(string hostName)
        {
            bool hostAvailable= false;
            try
            {
                TcpClient smtpTestClient = new TcpClient();
                smtpTestClient.Connect(hostName, 25);
                if (smtpTestClient.Connected)//connection is established
                {
                    NetworkStream netStream = smtpTestClient.GetStream();
                    StreamReader sReader = new StreamReader(netStream);
                    if (sReader.ReadLine().Contains("220"))//host is available for communication
                    {
                        hostAvailable= true;
                    }
                    smtpTestClient.Close();
                }
            }
            catch
            {
              //some action like writing to error log
            }
            return hostAvailable;
        }
    

提交回复
热议问题