/// </summary>
private string recvresponse()
{
int streamsize;
string returnvalue = string.empty;
byte[] readbuffer = new byte[1024] ;
try
{
streamsize=networkstream.read(readbuffer,0,readbuffer.length);
}
catch
{
errmsg="网络连接错误";
return "false";
}
if (streamsize==0)
{
return returnvalue ;
}
else
{
returnvalue = encoding.default.getstring(readbuffer).substring(0,streamsize);
logs+=returnvalue+this.crlf;
return returnvalue;
}
}
/// <summary>
/// 与服务器交互,发送一条命令并接收回应。
/// </summary>
/// <param name="str">一个要发送的命令</param>
/// <param name="errstr">如果错误,要反馈的信息</param>
private bool dialog(string str,string errstr)
{
if(str==null||str.trim()==string.empty)
{
return true;
}
if(sendcommand(str))
{
string rr=recvresponse();
if(rr=="false")
{
return false;
}
//检查返回的代码,根据[rfc 821]返回代码为3位数字代码如220
string rrcode=rr.substring(0,3);
if(rightcodeht[rrcode]!=null)
{
return true;
}
else
{
if(errcodeht[rrcode]!=null)
{
errmsg+=(rrcode+errcodeht[rrcode].tostring());
errmsg+=crlf;
}
else
{
errmsg+=rr;
}
errmsg+=errstr;
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// 与服务器交互,发送一组命令并接收回应。
/// </summary>
private bool dialog(string[] str,string errstr)
{
for(int i=0;i<str.length;i++)
{
if(!dialog(str[i],""))
{
errmsg+=crlf;
errmsg+=errstr;
return false;
}
}
return true;
}
//连接服务器
private bool connect(string smtpserver,int port)
{
//创建tcp连接
try
{
tcpclient=new tcpclient(smtpserver,port);
}
catch(exception e)
{
errmsg=e.tostring();
return false;
}
networkstream=tcpclient.getstream();
//验证网络连接是否正确
if(rightcodeht[recvresponse().substring(0,3)]==null)
{
errmsg="网络连接失败";
return false;
}
return true;
}
private string getprioritystring(mailpriority mailpriority)
{
string priority="normal";
if (mailpriority==mailpriority.low)
{
priority="low";
}
else if (mailpriority==mailpriority.high)
{
priority="high";
}
return priority;
}
/// <summary>
/// 发送电子邮件,smtp服务器不需要身份验证
/// </summary>
/// <param name="smtpserver"></param>
/// <param name="port"></param>