请大虾帮忙看看啊,没有任何错误提示,只是成功过一两次,发送的内容完全相同。
TcpClient tcpclient = new TcpClient();
            tcpclient.Connect(_host, _port);
            NetworkStream stream = tcpclient.GetStream();
            GetResponse(ref stream, ref tcpclient, 220);
            //SendCommand(Environment.NewLine, ref tcpclient, 220);
            SendCommand("helo localhost" + Environment.NewLine, ref tcpclient, 250);
            SendCommand("auth login" + Environment.NewLine, ref tcpclient, 334);
            SendCommand(Convert.ToBase64String(System.Text.Encoding.GetEncoding(936).GetBytes(_username)) + Environment.NewLine, ref tcpclient, 334);
            SendCommand(Convert.ToBase64String(System.Text.Encoding.GetEncoding(936).GetBytes(_password)) + Environment.NewLine, ref tcpclient, 235);
            SendCommand("mail from:<" + mailMessage.From.Address + ">" + Environment.NewLine, ref tcpclient, 250);
            SendCommand("rcpt to:<" + mailMessage.To.Address + ">" + Environment.NewLine, ref tcpclient, 250);
            SendCommand("data" + Environment.NewLine, ref tcpclient, 354);
            //SendCommand(getMessage(mailMessage) + Environment.NewLine + "." + Environment.NewLine, ref tcpclient, 250);
            SendMailMessage(mailMessage, ref tcpclient);
            SendCommand(Environment.NewLine + "." + Environment.NewLine, ref tcpclient, 250);
            SendCommand("quit" + Environment.NewLine, ref tcpclient, 0);
private void SendMailMessage(MailMessage mailMessage,ref TcpClient tcpClient)

解决方案 »

  1.   

    private void SendMailMessage(MailMessage mailMessage,ref TcpClient tcpClient)
            {
                Encoding _encode = Encoding.GetEncoding(mailMessage.CodePage);
                string v = string.Empty;
                if (mailMessage.Reply != null)
                    SendCommand("Reply-To:=?" + _encode.HeaderName + "?B?" + Convert.ToBase64String(_encode.GetBytes(mailMessage.Reply.Name)) + "?=<" + mailMessage.Reply.Address + ">" + Environment.NewLine,ref tcpClient,0);
                SendCommand("To:" + (mailMessage.To.Name != string.Empty ? ("=?" + _encode.HeaderName + "?B?" + Convert.ToBase64String(_encode.GetBytes(mailMessage.To.Name)) + "?= ") : string.Empty) + "<" + mailMessage.To.Address + ">" + Environment.NewLine,ref tcpClient,0);
                SendCommand("from:" + (mailMessage.From.Name != string.Empty ? ("=?" + _encode.HeaderName + "?B?" + Convert.ToBase64String(_encode.GetBytes(mailMessage.From.Name)) + "?= ") : string.Empty) + "<" + mailMessage.From.Address + ">" + Environment.NewLine,ref tcpClient,0);
                if (mailMessage.CC.Length != 0)
                {
                    v += "CC:";
                    for (int i = 0; i < mailMessage.CC.Length; i++)
                    {
                        v += " " + mailMessage.CC[i].Address;
                    }
                    v += Environment.NewLine;
                    SendCommand(v, ref tcpClient, 0);
                    v = string.Empty;
                    
                }
                if (mailMessage.Bcc.Length != 0)
                {
                    v += "BCC:";
                    for (int i = 0; i < mailMessage.Bcc.Length; i++)
                    {
                        v += " " + mailMessage.Bcc[i].Address;
                    }
                    v += Environment.NewLine;
                    SendCommand(v, ref tcpClient, 0);
                    v = string.Empty;
                }
                SendCommand("MIME-Version: 1.0" + Environment.NewLine, ref tcpClient, 0);
                SendCommand("subject:=?" + _encode.HeaderName + "?B?" + Convert.ToBase64String(_encode.GetBytes(mailMessage.Subject)) + "?=" + Environment.NewLine,ref tcpClient,0);
                SendCommand("Content-Type: multipart/mixed;" + Environment.NewLine,ref tcpClient,0);
                string flag = "--ZseaSmtp_" + Guid.NewGuid().ToString(); //定义分隔符
                SendCommand(" boundary=\"" + flag + "\"" + Environment.NewLine,ref tcpClient,0);
                SendCommand("x-ZseaSmtp-Version:1.0 beta" + Environment.NewLine + Environment.NewLine,ref tcpClient,0);
                SendCommand("This is a multi-part message in MIME format." + Environment.NewLine + Environment.NewLine,ref tcpClient,0);
                //SendCommand("--" + flag + Environment.NewLine, ref tcpClient, 0);
                //SendCommand("Content-Type: multipart/alternative;" + Environment.NewLine, ref tcpClient, 0);
                //SendCommand(" boundary=\"" + flag + "\"" + Environment.NewLine + Environment.NewLine + Environment.NewLine, ref tcpClient, 0);
                SendCommand("--" + flag + Environment.NewLine,ref tcpClient,0);
                SendCommand("Content-Type:" + (mailMessage.IsHtml ? "text/html" : "text/plain") + Environment.NewLine,ref tcpClient,0);
                SendCommand(" charset=\"" + _encode.HeaderName + "\"" + Environment.NewLine,ref tcpClient,0);
                SendCommand("Content-Transfer-Encoding: base64" + Environment.NewLine + Environment.NewLine,ref tcpClient,0);
                if (!string.IsNullOrEmpty(mailMessage.Body))
                {
                    v = Convert.ToBase64String(_encode.GetBytes(mailMessage.Body));
                    Regex r = new Regex(@".{1,76}", RegexOptions.IgnoreCase);
                    foreach (Match match in r.Matches(v))
                    {
                        SendCommand(match.Value + Environment.NewLine, ref tcpClient, 0);
                    }
                }
                SendCommand(Environment.NewLine+Environment.NewLine,ref tcpClient,0);
                //SendCommand("--" + flag + "--", ref tcpClient, 0);
                //开始发送附件
                Attachment[] attachmentList = mailMessage.getAttachment();
                if (attachmentList.Length > 0) //有附件
                {
                    SendCommand(Environment.NewLine,ref tcpClient,0);
                    foreach (Attachment attachment in attachmentList)
                    {
                        SendCommand("--" + flag + Environment.NewLine,ref tcpClient,0);
                        SendCommand("Content-Type: " + attachment.ContentType + ";" + Environment.NewLine,ref tcpClient,0);
                        SendCommand("   name=\"=?" + _encode.HeaderName + "?B?" + Convert.ToBase64String(_encode.GetBytes(attachment.Filename)) + "?=\"" + Environment.NewLine,ref tcpClient,0);
                        SendCommand("Content-Transfer-Encoding: base64" + Environment.NewLine,ref tcpClient,0);
                        SendCommand("Content-Disposition: attachment;" + Environment.NewLine,ref tcpClient,0);
                        SendCommand("   filename=\"=?" + _encode.HeaderName + "?B?" + Convert.ToBase64String(_encode.GetBytes(attachment.Filename)) + "?=\"" + Environment.NewLine,ref tcpClient,0);
                        SendCommand( Environment.NewLine,ref tcpClient,0);
                        v =attachment.Content ;
                        Regex r = new Regex(@".{1,76}", RegexOptions.IgnoreCase);
                        foreach (Match match in r.Matches(attachment.Content))
                        {
                            SendCommand(match.Value + Environment.NewLine, ref tcpClient, 0);
                        }
                        SendCommand(Environment.NewLine+Environment.NewLine, ref tcpClient, 0);
                    }
                }
                SendCommand("--" + flag + "--" ,ref tcpClient,0);
                //return v;
            }
      

  2.   

    看看防火墙
    还有接受的油箱支不支持stmp协议
      

  3.   

    代码过多没法分析,给你个建议吧发同样的邮件使用 Foxmail 等
    用抓包软件抓下来分析你和正常邮件程序发的包的区别
      

  4.   

    找到原因了,原来是有些信息在Stream中没有发出去