SMTP C#中的未知主机,System.Net.Sockets.SocketException

时间:2016-11-23 02:11:48

标签: c# sockets smtp telnet

我的代码发送此错误,System.Net.Sockets.SocketException因为无法识别主机,无论如何我正在使用localhost ...我尝试通过CMD初始化telnet,实际上端口25正在使用。< / p>

错误行:

TcpClient SmtpServ = new TcpClient(ServSMTP.Text,25);

    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
        Application.Run(new Form1());
    }

    private void SendBtn_Click(object sender, System.EventArgs e)
    {
        // change cursor into wait cursor
        Cursor cr = Cursor.Current;
        Cursor.Current = Cursors.WaitCursor;

        // create server SMTP with port 25
        TcpClient SmtpServ = new TcpClient(ServSMTP.Text,25);
        string Data;
        byte[] szData;
        string CRLF = "\r\n";
        LogList.Items.Clear();          

        try
        {
            // initialization
            NetworkStream NetStrm = SmtpServ.GetStream();
            StreamReader  RdStrm= new StreamReader(SmtpServ.GetStream());
            LogList.Items.Add(RdStrm.ReadLine());

            // say hello to server and send response into log report
            Data = "HELLO server " + CRLF;              
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());

            // send sender data
            Data = "MAIL FROM: " + "<" + sFrom.Text + ">" + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());

            // send receiver data
            Data = "RCPT TO: " + "<" + sTo.Text + ">" + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());

            // send DATA
            Data = "DATA " + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());               

            // send content data
            Data = "SUBJECT: " + sSubject.Text + CRLF + sMessage.Text + CRLF + "." + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());               

            // quit from server SMTP
            Data = "QUIT " + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());               

            // close connection
            NetStrm.Close();
            RdStrm.Close();
            LogList.Items.Add("Close connection");
            LogList.Items.Add("Send mail successly..");

            // back to normal cursor
            Cursor.Current = cr;
        }
        catch(InvalidOperationException err)
        {
            LogList.Items.Add("Error: "+ err.ToString());
        }
    }
}
}

0 个答案:

没有答案