请问C#中如何编写SOCKET连接,如有现成代码望给予提供,谢谢!!!

解决方案 »

  1.   


            private bool SendEmail()
            {
                //连接网络 
                try
                {
                    tc = new TcpClient(mailserver, mailserverport);
                }
                catch (Exception e)
                {
                    errmsg = e.ToString();
                    return false;
                }            ns = tc.GetStream();
                SMTPCodeAdd();            //验证网络连接是否正确 
                if (RightCodeHT[RecvResponse().Substring(0, 3)] == null)
                {
                    errmsg = "网络连接失败";
                    return false;
                }
                string[] SendBuffer;
                string SendBufferstr;            //进行SMTP验证 
                if (ESmtp)
                {
                    SendBuffer = new String[4];
                    SendBuffer[0] = "EHLO " + mailserver + enter;
                    SendBuffer[1] = "AUTH LOGIN" + enter;
                    SendBuffer[2] = Base64Encode(username) + enter;
                    SendBuffer[3] = Base64Encode(password) + enter;
                    if (!Dialog(SendBuffer, "SMTP服务器验证失败,请核对用户名和密码。"))
                        return false;
                }
                else
                {
                    SendBufferstr = "HELO " + mailserver + enter;
                    if (!Dialog(SendBufferstr, ""))
                        return false;
                }            // 
                SendBufferstr = "MAIL FROM:<" + From + ">" + enter;
                if (!Dialog(SendBufferstr, "发件人地址错误,或不能为空"))
                    return false;            // 
                SendBuffer = new string[recipientmaxnum];
                for (int i = 0; i < Recipient.Count; i++)
                {                SendBuffer[i] = "RCPT TO:<" + Recipient[i].ToString() + ">" + enter;            }
                if (!Dialog(SendBuffer, "收件人地址有误"))
                    return false;            SendBufferstr = "DATA" + enter;
                if (!Dialog(SendBufferstr, ""))
                    return false;            SendBufferstr = "From:" + FromName + "<" + From + ">" + enter;            SendBufferstr += "To:=?" + Charset.ToUpper() + "?B?" + Base64Encode(RecipientName) + "?=" + "<" + Recipient[0] + ">" + enter;
                SendBufferstr += "CC:";
                for (int i = 0; i < Recipient.Count; i++)
                {
                    SendBufferstr += Recipient[i].ToString() + "<" + Recipient[i].ToString() + ">,";
                }
                SendBufferstr += enter;
                if (Charset == "")
                {
                    SendBufferstr += "Subject:" + Subject + enter;
                }
                else
                {
                    SendBufferstr += "Subject:" + "=?" + Charset.ToUpper() + "?B?" + Base64Encode(Subject) + "?=" + enter;
                }            SendBufferstr += "X-Priority:" + priority + enter;
                SendBufferstr += "X-MSMail-Priority:" + priority + enter;
                SendBufferstr += "Importance:" + priority + enter;
                SendBufferstr += "X-Mailer: Huolx.Pubclass" + enter;
                SendBufferstr += "MIME-Version: 1.0" + enter;            SendBufferstr += "Content-Type: multipart/mixed;" + enter;//内容格式和分隔符 
                SendBufferstr += "   boundary=\"----=_NextPart_000_00D6_01C29593.AAB31770\"" + enter;
                SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770" + enter;            if (Html)
                {
                    SendBufferstr += "Content-Type: text/html;" + enter;
                }
                else
                {
                    SendBufferstr += "Content-Type: text/plain;" + enter;
                }            if (Charset == "")
                {
                    SendBufferstr += "   charset=\"gb2312\"" + enter;
                }
                else
                {
                    SendBufferstr += "   charset=\"" + Charset.ToLower() + "\"" + enter;
                }
             //   SendBufferstr += "Content-Transfer-Encoding: base64"+enter; 
                SendBufferstr += "Content-Transfer-Encoding: base64" + enter + enter;
                SendBufferstr += Base64Encode(Body) + enter + enter;
                if (Attachments.Count != 0)
                {
                    foreach (string filepath in Attachments)
                    {
                        SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770" + enter;
                        SendBufferstr += "Content-Type: application/octet-stream" + enter;
                        SendBufferstr += "   name=\"=?" + Charset.ToUpper() + "?B?" + Base64Encode(filepath.Substring(filepath.LastIndexOf("\\") + 1)) + "?=\"" + enter;
                        SendBufferstr += "Content-Transfer-Encoding: base64" + enter;
                        SendBufferstr += "Content-Disposition: attachment;" + enter;
                        SendBufferstr += "   filename=\"=?" + Charset.ToUpper() + "?B?" + Base64Encode(filepath.Substring(filepath.LastIndexOf("\\") + 1)) + "?=\"" + enter + enter;
                        SendBufferstr += GetStream(filepath) + enter + enter;
                    }
                }
                SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770--" + enter + enter;
               // SendBufferstr += "------=_NextPart_821375_13095514.1180514691160--" + enter + enter;
               
                SendBufferstr += enter + "." + enter;            if (!Dialog(SendBufferstr, "错误信件信息"))
                    return false;
                SendBufferstr = "QUIT" + enter;
                if (!Dialog(SendBufferstr, "断开连接时错误"))
                    return false;
                ns.Close();
                tc.Close();
                return true;
            }
      

  2.   

    一个异步Sockethttp://blog.csdn.net/LeoMaya/archive/2007/08/06/1728518.aspx