1

在当前项目中,我被要求通过“Helo”的响应检查与 Smtp 服务器的连接(“Ehlo”也可以,实际上,我可以听取任何可以确认连接的建议)。我问谷歌是否有一些简单的方法可以相当于在 C# 中键入这两个简单的命令。我在一些编码方面有一点经验TcpClient,所以我知道如何将字符串作为字节数据发送。

telnet smtp.gmail.com 465
helo localhost

但是......,NET的SmtpClient班级似乎没有Stream成员!在这种情况下,我怎么能发送一些命令?

4

3 回答 3

3

用于TcpClient连接到远程端口,然后将helo localhost命令作为一串 ASCII 字符发送。

        TcpClient client = new TcpClient("smtp.gmail.com", 465);

        var stream = client.GetStream();

        var bytes = Encoding.ASCII.GetBytes("localhost helo");

        stream.Write(bytes, 0, bytes.Length);

但是,我相信端口 465 用于安全通信,因此您需要使用 SSL 或使用端口 25。

于 2013-05-15T14:31:46.523 回答
3
public static class SmtpHelper
    {
        /// <summary>
        /// test the smtp connection by sending a HELO command
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public static bool TestConnection(Configuration config)
        {
            MailSettingsSectionGroup mailSettings = config.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
            if (mailSettings == null)
            {
                throw new ConfigurationErrorsException("The system.net/mailSettings configuration section group could not be read.");
            }
            return TestConnection(mailSettings.Smtp.Network.Host, mailSettings.Smtp.Network.Port);
        }

        /// <summary>
        /// test the smtp connection by sending a HELO command
        /// </summary>
        /// <param name="smtpServerAddress"></param>
        /// <param name="port"></param>
        public static bool TestConnection(string smtpServerAddress, int port)
        {
            IPHostEntry hostEntry = Dns.GetHostEntry(smtpServerAddress);
            IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], port);
            using (Socket tcpSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
            {
                //try to connect and test the rsponse for code 220 = success
                tcpSocket.Connect(endPoint);
                if (!CheckResponse(tcpSocket, 220))
                {
                    return false;
                }

                // send HELO and test the response for code 250 = proper response
                SendData(tcpSocket, string.Format("HELO {0}\r\n", Dns.GetHostName()));
                if (!CheckResponse(tcpSocket, 250))
                {
                    return false;
                }

                // if we got here it's that we can connect to the smtp server
                return true;
            }
        }

        private static void SendData(Socket socket, string data)
        {
            byte[] dataArray = Encoding.ASCII.GetBytes(data);
            socket.Send(dataArray, 0, dataArray.Length, SocketFlags.None);
        }

        private static bool CheckResponse(Socket socket, int expectedCode)
        {
            while (socket.Available == 0)
            {
                System.Threading.Thread.Sleep(100);
            }
            byte[] responseArray = new byte[1024];
            socket.Receive(responseArray, 0, socket.Available, SocketFlags.None);
            string responseData = Encoding.ASCII.GetString(responseArray);
            int responseCode = Convert.ToInt32(responseData.Substring(0, 3));
            if (responseCode == expectedCode)
            {
                return true;
            }
            return false;
        }
    }

用法:

if (!SmtpHelper.TestConnection(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)))
{
    throw new ApplicationException("The smtp connection test failed");
}
于 2013-05-15T14:39:31.120 回答
0

使用 C# Telnet发送命令HELO <host>

于 2019-06-04T23:45:28.747 回答