接上贴:
sendFeedback (ref netStream,"421 unsuccessful,connection will be Closed");
newClient.Close();
}
}
else if(command=="mail")
{
try
{
mailfrom=true;
from=parameter;
rcptto=false;
sendFeedback(ref netStream, "250 "+parameter+"...sender OK!");
}
catch{sendFeedback(ref netStream,"502 performed unsuccessful");}
}
else if(command=="rcpt")
{
bool round =true;
int i=0;
try
{
if(mailfrom==true)
{
rcptto=true;
string host=splitServer(parameter);
mailbox=splitMailbox(parameter);
if(host==hostName)
{
bool mailboxExists=Directory.Exists("e:\\smtp\\mailbox\\"+mailbox);
if(mailboxExists==true)
{
while((round==true)&&(i<1024))
{
bool mailExists=File.Exists("e:\\smtp\\mailbox\\"+mailbox+"\\"+"mail"+i.ToString()+".txt");
if(mailExists==false)
{
queue=i;
mail=new StreamWriter("e:\\smpt\\mailbox\\"+mailbox+"\\"+"mail"+queue.ToString()+".txt",false,System.Text.Encoding.UTF8);
mail.Write("\r\n");
mail.Close();
round=false;
}
i++;
}
sendFeedback(ref netStream,"250 "+parameter);
if(round==true)
{
sendFeedback(ref netStream,"452 "+parameter+"space insufficient");
}
}
else
{
sendFeedback(ref netStream,"550 "+parameter+"does not exist");
}
}
else
{
//下面可添加代码调动转发功能
//上面可添加代码调用转发功能
}
}
else
{
sendFeedback(ref netStream,"503 mail from command needed");
}
}
catch{sendFeedback(ref netStream,"502 performed unsuccessful");}
}
else if(command=="data"|| getMail==true)
 {
 try
 {
 if((mailfrom==true)&&(rcptto==true))
 {
 bool round=true;
 if(getMail==false)
 {
 sendFeedback(ref netStream,"354 input:end with<CR><LF>.<CR><LF>");
 getMail=true;
 }
 while(round==true)
 {
 string message=readMail(ref netStream);
 int checkEnd=message.IndexOf("\r\n.\r\n");
 if(checkEnd==-1)
 {
 mail=new StreamWriter("e:\\smtp\\mailbox\\"+mailbox+"\\"+"mail"+queue.ToString()+".txt",true,System.Text.Encoding.UTF8);
 mail.Write(message);
 mail.Close();
 }
 else
 {
 mail=new StreamWriter("e:\\smtp\\mailbox\\"+mailbox+"\\"+"mail"+queue.ToString()+".txt",true,System.Text.Encoding.UTF8);
 mail.Write(message);
 mail.Close();
 round=false;
 getMail=false;
 }
 }
 sendFeedback(ref netStream,"250 OK");
 }
 else{sendFeedback(ref netStream,"503 MAIL or RCPT command needed");}
 }
 catch{sendFeedback(ref netStream,"502 performed unsuccessful");}
 }
 else if(command=="help")
 {
 thread.Resume();
 try
 {
 sendFeedback(ref netStream,"211 free smtp server ..and other help message.");}
 catch
 {
 sendFeedback(ref netStream,"502 performedunsuccessful,try again");
 }
 }
 else if(command=="quit")
 {
 try
 {
 control=true;
 sendFeedback(ref netStream,"211 connection closed");
 newClient.Close();
 }
 catch{sendFeedback(ref netStream,"502 performed unsuccessful,try again");}
 }
 else if(getMail==false)
 {
 sendFeedback(ref netStream,"500 unrecognized command");
 }
} }
private string readCommand (ref NetworkStream NetStream)
{
byte[] byteMessage=new byte[1024];
NetStream.Read(byteMessage,0,byteMessage.Length);
string command=System.Text.Encoding.UTF8.GetString(byteMessage);
return command;
}
private void sendFeedback(ref NetworkStream NetStream,string ToSend)
{
string stringToSend = ToSend+"\r\n";
byte[] arrayToSend=System.Text.Encoding.UTF8.GetBytes(stringToSend.ToCharArray());
NetStream.Write(arrayToSend,0,arrayToSend.Length);
NetStream.Flush();
}
private string readMail (ref NetworkStream NetStream)
{
byte[] byteArray=new Byte[1024];
NetStream.Read(byteArray,0,byteArray.Length);
string mailMessage=System.Text.Encoding.UTF8.GetString(byteArray);
return mailMessage;
}
private string splitServer (string aimString)
{
string[] aim=new string[2];
char[] a=new char[]{'@'};
aim=aimString.Split(a);
int y=aim[1].IndexOf(">");
if(y!=-1){
string server=aim[1].Substring(0,aim[1].Length-1);
return server;
}
else{
return aim[1];}
}
private string splitMailbox(string aimString)
{
string[] aim=new string[2];
char[] a=new char[]{'@'};
aim=aimString.Split(a);
int y=aim[0].IndexOf("<");
if(y!=-1)
{
string mailbox=aim[0].Substring(y+1,aim[0].Length-y-1);
return mailbox;
}
else{
int x=aim[0].IndexOf(":");
string mailbox=aim[0].Substring(x+1,aim[0].Length-x-1);
return mailbox;
}
}
private string splitCommand(string aimString)
{
string[] aim=new string[2];
int x=aimString.IndexOf(" ");
if(x!=-1)
{
char[] a=new char[]{' '};
aim=aimString.Split(a);
return aim[0];}
else{return aimString;}
}
private string splitParameter(string aimString)
{
string[] aim=new string[2];
int x=aimString.IndexOf(" ");
if(x!=-1)
{
string para=aimString.Substring(x+1,aimString.Length -x-1);
return para;
}
else{return "";}
}
private string bigCommand(string commString)
{
int x=commString.IndexOf("\r\n");
string command=commString.Substring(0,x);
return command;
}
}
}

解决方案 »

  1.   

    通过你这句代码:newClient=listener.AcceptSocket();
        说明你要把listener对象的AcceptSocket()方法作为一个对象来处理,这个对象起名为newClient,你直接给newClient对象赋值,在此之前却没有任何对newClient对象的定义。你得先引用newClient类型类所在的命名空间,然后定义一个名为newClient的对象。    具体是什么命名空间,我也不知道,我没用过网络通信方面的类,你自己查查文档吧。
        不过也有可能根本没有相应的命名空间,你得自己写相应的类:(,你就得查查listener.AcceptSocket();这个方法返回的是什么东西了。
      

  2.   

    我查到了,应该是返回一个Socket对象也就是说,把你的代码
    newClient=listener.AcceptSocket();改为
    Socket newClient = listener.AcceptSocket();
    试试
      

  3.   

    或在form2的顶部加个
    Socket  newClient;
    试试
      

  4.   

    一定要在顶部加,
    代码改成Socket newClient = listener.AcceptSocket();还是会出错
      

  5.   

    那儿搞来的代码,这么多错误port没有定义
    newClient也没有定义把头部的private int port; 改为 private int port = 100;
    并且头部也要加上Socket newClient = listener.AcceptSocket();
    这样编译至少能过
      

  6.   

    上面由于一时激动,说错了,在头部加Socket newClient ,而不是Socket newClient = listener.AcceptSocket();